0
0
MlopsHow-ToBeginner · 3 min read

How to Load a Model in sklearn: Simple Guide

To load a saved model in sklearn, use joblib.load() or pickle.load() on the file where the model was saved. This restores the model object so you can use it for predictions or further training.
📐

Syntax

Use joblib.load(filename) or pickle.load(file_object) to load a saved sklearn model.

  • filename: Path to the saved model file.
  • file_object: Opened file in binary read mode for pickle.

joblib is preferred for sklearn models because it handles large numpy arrays efficiently.

python
from joblib import load

model = load('model_filename.joblib')
💻

Example

This example shows how to save a simple sklearn model and then load it back to make predictions.

python
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from joblib import dump, load

# Load data
iris = load_iris()
X, y = iris.data, iris.target

# Train model
model = RandomForestClassifier(random_state=42)
model.fit(X, y)

# Save model
dump(model, 'rf_model.joblib')

# Load model
loaded_model = load('rf_model.joblib')

# Predict with loaded model
predictions = loaded_model.predict(X[:5])
print(predictions)
Output
[0 0 0 0 0]
⚠️

Common Pitfalls

1. Using pickle without binary mode: When loading with pickle, always open the file in binary mode 'rb'.

2. File path errors: Ensure the file path is correct and accessible.

3. Version mismatch: Loading a model saved with a different sklearn or Python version can cause errors.

python
import pickle

# Wrong way (missing binary mode)
# with open('model.pkl', 'r') as f:
#     model = pickle.load(f)  # This will raise an error

# Correct way
with open('model.pkl', 'rb') as f:
    model = pickle.load(f)
📊

Quick Reference

MethodUsageNotes
joblib.loadmodel = joblib.load('file.joblib')Preferred for sklearn models, handles numpy arrays well
pickle.loadwith open('file.pkl', 'rb') as f: model = pickle.load(f)Works but requires careful file mode handling
File pathUse correct relative or absolute pathCommon source of errors
Version compatibilityMatch sklearn and Python versionsAvoids loading errors

Key Takeaways

Use joblib.load() to load sklearn models efficiently.
Always open files in binary mode when using pickle.load().
Check file paths carefully to avoid file not found errors.
Model loading can fail if sklearn or Python versions differ.
After loading, the model is ready for prediction or further use.