Model Persistence and Deployment

Scikit-learn Basics

2 min read

Published Nov 17 2025, updated Nov 19 2025


11
0
0
0

ClusteringFeature EngineeringK-MeansLinear RegressionLogistic RegressionMachine LearningNumPyPythonRandom Forestsscikit-learnSupervised LearningUnsupervised Learning

Once a model has been trained and validated, the next step is to preserve and reuse it.
Model persistence lets you avoid retraining every time, and deployment allows the model to make predictions on new, unseen data, often in an application, API, or scheduled process.


Scikit-learn integrates smoothly with Python’s standard tools for persistence (joblib, pickle) and plays nicely with web frameworks and workflow systems for deployment.






Why Persist Models?

  • Reproducibility : Reload the exact same model months later.
  • Speed : Skip retraining for every session or service restart.
  • Consistency : Ensure training and inference use identical preprocessing.
  • Deployment : Serve predictions through APIs or batch processes.

Typical life-cycle:

  1. Train
  2. Validate
  3. Save
  4. Deploy
  5. Predict
  6. Monitor
  7. Retrain





Saving and Loading Models with joblib

joblib is recommended over pickle for Scikit-learn objects, it’s faster and handles large NumPy arrays efficiently.

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import joblib

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

model = RandomForestClassifier(random_state=42).fit(X_train, y_train)

# Save model
joblib.dump(model, "iris_model.pkl")

# Load model
loaded_model = joblib.load("iris_model.pkl")
print("Loaded model accuracy:", loaded_model.score(X_test, y_test))

Output:

Loaded model accuracy: 1.0

Notes:

  • The file extension doesn’t matter, but .pkl or .joblib are common.
  • Always load with the same Scikit-learn version used to train it.





Saving Pipelines (Preferred Way)

If you’ve built a preprocessing + model pipeline, save the entire pipeline rather than individual components.

joblib.dump(pipeline, "customer_pipeline.pkl")

# Later
pipe = joblib.load("customer_pipeline.pkl")
pred = pipe.predict(new_data)

This guarantees preprocessing and model steps stay synchronised.






Versioning and Reproducibility

A saved model should always be accompanied by metadata describing:

  • Scikit-learn version
  • Python version
  • Feature names / preprocessing steps
  • Training data summary
  • Hyperparameters

Example:

import json, sklearn
metadata = {
    "sklearn_version": sklearn.__version__,
    "model_type": type(model).__name__,
    "train_shape": X_train.shape,
    "params": model.get_params()
}
json.dump(metadata, open("model_metadata.json", "w"), indent=2)

Store these files together so you can rebuild or audit the model later.






Making Predictions After Loading

Once loaded, use the model exactly like before:

new_obs = [[5.0, 3.6, 1.4, 0.2]]
prediction = loaded_model.predict(new_obs)
print("Predicted class:", prediction)

If it was a pipeline, preprocessing (scaling, encoding, etc.) will happen automatically.






Deploying a Model as a Script

For local or batch use, you can create a small Python script:

# predict_customer.py
import joblib, sys, pandas as pd

model = joblib.load("customer_pipeline.pkl")
data = pd.read_csv(sys.argv[1])
preds = model.predict(data)
pd.DataFrame(preds, columns=["prediction"]).to_csv("predictions.csv", index=False)

Run:

python predict_customer.py new_data.csv





Monitoring Deployed Models

After deployment, monitor:

  • Input drift: Data distribution shifts over time.
  • Performance decay: Accuracy declines on recent data.
  • Service metrics: Latency, error rates, uptime.

Tools like Evidently AI, MLflow Monitoring, or custom dashboards can automate drift and accuracy checks.


Example idea:

# Pseudo-code
if current_accuracy < baseline * 0.95:
    alert("Model drift detected")





Security and Integrity Considerations

  • Never load models from untrusted sources (pickle/joblib can execute code).
  • Sign or checksum your model files if distributed externally.
  • Restrict API input formats to prevent code injection or denial-of-service attacks.




Best Practices

  1. Save the full pipeline, not just the model.
  2. Document versions and parameters.
  3. Use joblib for large models.
  4. Keep metadata with the model artifact.
  5. Monitor post-deployment drift.
  6. Automate retraining when performance degrades.

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact