A model is only useful once it can be reloaded for predictions outside the training notebook.
Fitted estimators are Python objects, so the standard way to save them is pickling. joblib.dump(model, "model.joblib") and joblib.load("model.joblib") are recommended over plain pickle because they handle large NumPy arrays efficiently.
Save the whole pipeline, including preprocessing, not just the final model. Then the serving code only calls model.predict(df) with raw input columns.
Things to know before relying on a saved model:
- Security: loading a pickle can execute arbitrary code. Only load files you created or trust. The
skops.iolibrary offers a safer format that refuses unknown types unless you trust them explicitly. - Versions: pickles are only guaranteed to load with the same scikit-learn version (and should use the same NumPy/SciPy). Loading with another version emits
InconsistentVersionWarningand may break or, worse, silently change predictions. Pin library versions in the serving environment and record them next to the model. - Custom code: if the pipeline uses your own functions or classes, they must be importable under the same module path when loading.
For serving across languages or with lower latency, models can be converted to ONNX with the separate skl2onnx package. Whatever the format, store metadata with the file: training date, data version, library versions and validation scores.
Example
import joblib, sklearn
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = load_iris(return_X_y=True, as_frame=True)
pipe = make_pipeline(StandardScaler(), LogisticRegression()).fit(X, y)
joblib.dump({"model": pipe, "sklearn": sklearn.__version__}, "iris.joblib")
bundle = joblib.load("iris.joblib") # only load files you trust
print(bundle["sklearn"]) # 1.9.1
print(bundle["model"].predict(X.iloc[:3])) # [0 0 0]Remember
- Save the full pipeline with
joblib.dump - Load pickles only from trusted sources; consider skops.io
- Pin and record the scikit-learn version used for training
Watch out for
- Loading a model with a different scikit-learn version and ignoring InconsistentVersionWarning.