Replies: 4 comments
|
Yes β when you pickle the When you load it back, you can use it directly on new data:
You do not need to manually reapply Just note: scikit-learn recommends saving models using joblib instead of pickle:
|
|
Hi! When you serialize a scikit-learn This means that:
When you load the pickled pipeline back and call Exampleimport pickle
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.naive_bayes import GaussianNB
# Fit and save
pipe = make_pipeline(StandardScaler(), GaussianNB())
pipe.fit(X_train, y_train)
with open('my_model.pkl', 'wb') as file:
pickle.dump(pipe, file)
# Later... Load and predict directly
with open('my_model.pkl', 'rb') as file:
loaded_pipe = pickle.load(file)
# The loaded_pipe will automatically scale X_test using the scale/mean of X_train!
predictions = loaded_pipe.predict(X_test)For large scikit-learn models (especially those containing numpy arrays like random forests), it is highly recommended to use import joblib
joblib.dump(pipe, 'my_model.joblib')
loaded_pipe = joblib.load('my_model.joblib') |
|
Hi! When you serialize a scikit-learn This means that:
When you load the pickled pipeline back and call Exampleimport pickle
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.naive_bayes import GaussianNB
# Fit and save
pipe = make_pipeline(StandardScaler(), GaussianNB())
pipe.fit(X_train, y_train)
with open('my_model.pkl', 'wb') as file:
pickle.dump(pipe, file)
# Later... Load and predict directly
with open('my_model.pkl', 'rb') as file:
loaded_pipe = pickle.load(file)
# The loaded_pipe will automatically scale X_test using the scale/mean of X_train!
predictions = loaded_pipe.predict(X_test)For large scikit-learn models (especially those containing numpy arrays like random forests), it is highly recommended to use import joblib
joblib.dump(pipe, 'my_model.joblib')
loaded_pipe = joblib.load('my_model.joblib') |
|
Yes, the fitted Pipeline is serialized as one estimator, including the StandardScaler, its learned mean/scale, and the classifier. After loading it, call predict(X_new) directly; do not scale X_new manually. For example: from joblib import dump, load dump(pipe, "model.joblib") Two practical caveats: only load pickle/joblib files from trusted sources because deserialization can execute arbitrary code, and keep the scikit-learn and dependency versions compatible with the environment used for training. |
Uh oh!
There was an error while loading. Please reload this page.
I'm comparing a number of classifiers & want to save the models. For instance,
My question is, when I come to use those models, will the pipe line be part of the loaded model or with the pickle file just contain the model parameters & will have to apply the
StandardScaler()function to any input data?All reactions