AI Hints
Home›Machine Learning Tutorial›Shipping Your Model
Machine Learning · Lesson 58 of 85

Saving Your Model

From a notebook to a file.

⏱ 2 min read▶ Video at 2:04:02🎓 Free
Watch this lesson in the Machine Learning courseWatch on YouTube ↗Full Machine Learning course

Here is a painful truth. Most models never reach a real user. They die in a notebook, on someone's laptop. Your boss doesn't want a chart, your boss wants a working product. So let's get your model out of the lab, and the first step is simple. Save it.

Saving Your Model
From the video: Saving Your Model
  • Most models die in a notebook
  • Your boss wants a product
  • Step one: save it

Saving and loading

Training took some time, and we don't want to repeat it for each customer. Joblib saves the whole fitted model to a single file. Later, in another program, on another computer, joblib load brings it back, instantly. We check that the original and loaded models give identical predictions. Run it. They match exactly. The model is now just a file that you can copy, store, and version.

save_model.pypython
import joblib
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import HistGradientBoostingRegressor

data = fetch_california_housing(as_frame=True)
model = HistGradientBoostingRegressor(random_state=0).fit(data.data, data.target)

joblib.dump(model, "homeworth.joblib")
loaded = joblib.load("homeworth.joblib")

print("Original:", model.predict(data.data.iloc[:2]).round(3))
print("Loaded:  ", loaded.predict(data.data.iloc[:2]).round(3))
Output
Original: [4.155 3.859]
Loaded:   [4.155 3.859]

Three rules for model files

Three rules will save you from pain. First, save the whole pipeline, not only the model, because the scaler and encoders must travel with it. Second, record the versions. A file made with one version of scikit learn may not load in another, so keep a requirements file. And third, only load files that you trust. A joblib file can run code when it is opened, so never load one from a stranger.

Save the whole pipelinepreprocessing travels with it
Record versionspin your requirements
Only trust your own filesloading can run code

Summary

Your model is now a file. But a file can't help customers. They need something they can talk to. The standard answer is an A P I, and you can build one in about twenty lines. That's next.

Key takeaways
  • joblib saves fitted pipelines
  • Pin your library versions
  • Never load untrusted model files
Next up: Serving with an API — twenty lines to production