Let's say I used minmaxscaler while creating my model. Now, i'm loading that model via Pickle in a Flask app. Upon receiving a request containing a datapoint I would like to apply to it the same transformations that I applied to my training dataset before calling the predict() method. How do I transfer that set of transformations from one file to a webservice?
Rather than storing and loading many files, create a Scikit-learn transformation pipeline with all of your transformations, and then save that as a pickle or joblib file.
from sklearn.pipeline import Pipeline from sklearn.externals import joblib pipeline = Pipeline([ ('normalization', MinMaxScaler()), ('classifier', RandomForestClassifier()) ]) joblib.dump(pipeline, 'transform_predict.joblib')
You can then just load one transformation pipeline and call fit_transform to transform the input data and get predictions for it:
pipeline = load('transform_predict.joblib') predictions = pipeline.predict(new_data)
You need to save minmaxscaler (along with model). In Flask app, you can :
- Load scaler from file
- Use this instance of scaler for scaling input values
from sklearn.externals import joblib scaler_filename = "saved_scaler" joblib.dump(scaler, scaler_filename)
In Flask App
scaler_filename = "saved_scaler" scaler = joblib.load(scaler_filename)