Saving Models
Python · Machine Learning (Scikit-Learn)
Theory
Training can take minutes or hours, so you save the fitted model and load it later to predict without retraining. scikit-learn models are saved with joblib: joblib.dump(model, 'loan_model.joblib') writes the object to a file and joblib.load('loan_model.joblib') restores it, ready for predict. Save the whole Pipeline, not just the final estimator, so the exact same imputing, encoding and scaling are applied to new data.
Two cautions. A saved file is tied to the library versions that created it, so load it with the same scikit-learn version and record the versions alongside the file. And joblib and pickle files can run arbitrary code when loaded, so never load a model file from an untrusted source. Store useful metadata with the model too, such as the feature names, training date and test score.
Why we use it
Saving separates training from serving: you train once, then a web app, batch job or teammate loads the model and predicts instantly. Saving the pipeline guarantees predictions use exactly the preprocessing that was evaluated.
When to use it
Save after final evaluation, whenever a model will be reused or deployed. Keep the scikit-learn version with the file and retrain or re-save when you upgrade. Do not load model files you did not create or cannot trust.
How it works
- Model evaluated and final
- Bundle the pipeline with metadata (features, version)
- joblib.dump(bundle, 'model.joblib')
- Is the file from a trusted source and same sklearn version? Yes: joblib.load and predict. No: Retrain instead of loading.
- Predictions without retraining
Syntax
import joblib
joblib.dump(pipeline, 'model.joblib')
model = joblib.load('model.joblib')
model.predict(X_new)Example
joblib.dump(clf, 'loan_model.joblib')
loaded = joblib.load('loan_model.joblib')
print(loaded.predict(X_test[:5]))Key points
- joblib.dump / joblib.load save and restore fitted models
- Save the whole Pipeline, including preprocessing
- Load with the same scikit-learn version that saved it
- Never load model files from untrusted sources
- Store feature names, version and metrics with the model