scikit-learn
machine learning
model persistence
classifier
Python

Save classifier to disk in scikit-learn

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Persisting a trained scikit-learn classifier lets you reuse it for prediction without retraining. The usual choice is joblib, but the right answer depends on what you care about: performance, security, portability, and whether you are saving only the classifier or the full preprocessing pipeline.

The Standard Approach with joblib

For trusted internal artifacts, joblib is still the most common way to save a fitted estimator.

python
1from joblib import dump, load
2from sklearn.datasets import load_iris
3from sklearn.ensemble import RandomForestClassifier
4
5X, y = load_iris(return_X_y=True)
6clf = RandomForestClassifier(random_state=42)
7clf.fit(X, y)
8
9dump(clf, "model.joblib")
10loaded = load("model.joblib")
11print(loaded.predict(X[:2]))

joblib is efficient for objects that contain large NumPy arrays, which is why it is frequently recommended for scikit-learn models.

Save the Whole Pipeline, Not Just the Classifier

In production, the classifier is often only half the story. If the training code used scaling, encoding, or feature extraction, save the fitted pipeline instead of saving only the final estimator.

python
1from joblib import dump
2from sklearn.pipeline import Pipeline
3from sklearn.preprocessing import StandardScaler
4from sklearn.svm import SVC
5
6pipeline = Pipeline([
7    ("scale", StandardScaler()),
8    ("clf", SVC())
9])
10
11pipeline.fit(X, y)
12dump(pipeline, "pipeline.joblib")

Saving the pipeline prevents training-serving skew, where the prediction service forgets to apply the same preprocessing that the model saw during training.

Security and Version Compatibility

This part matters. joblib, pickle, and similar formats should only be loaded from trusted sources because they can execute arbitrary code during deserialization.

Scikit-learn documentation also warns that loading a model across different scikit-learn versions is not a supported portability guarantee. A model saved under one library version may appear to load under another, but relying on that is a bad deployment strategy.

The safest operational pattern is to store these together:

  • the serialized model file
  • the training code revision
  • the Python version
  • the scikit-learn and dependency versions

That makes rebuilding the same environment much easier.

When to Consider Another Format

If you need a safer format than pickle-based persistence, the scikit-learn documentation points to skops.io. If you only need portable inference and your model is supported, ONNX can be a better deployment format than a Python object dump.

Those options are more situational, but they are worth knowing about when security or serving portability matters.

Loading the Model for Prediction

A prediction script is straightforward:

python
1from joblib import load
2
3model = load("pipeline.joblib")
4sample = [[5.1, 3.5, 1.4, 0.2]]
5print(model.predict(sample))

The key is that the runtime environment should match the environment that created the file closely enough to avoid compatibility surprises.

Save Metadata Beside the Model

In practice, teams often store a small metadata file next to the serialized model. That file can record the training date, feature list, label mapping, and dependency versions. The model file alone tells you how to predict, but the metadata explains what the artifact actually means.

Common Pitfalls

The most common mistake is saving only the classifier and forgetting the preprocessing steps.

Another problem is loading model files from untrusted locations. Pickle-based formats are not safe for that.

A third pitfall is assuming model files are portable across arbitrary Python or scikit-learn versions. In practice, version pinning matters.

Summary

  • 'joblib.dump and joblib.load are the standard scikit-learn persistence tools.'
  • Save the full pipeline when preprocessing is part of the model behavior.
  • Treat serialized model files as trusted-code artifacts, not harmless data.
  • Keep library and Python versions with the artifact for repeatable loading.
  • Consider skops.io or ONNX when security or serving portability is more important than raw convenience.

Course illustration
Course illustration

All Rights Reserved.