Saving and Loading a Model

joblib.dump writes a trained model to a file and joblib.load brings it back with the same guesses, as long as you save the whole pipeline, load it with the same scikit-learn version it was saved with, and only ever load files you or someone you trust made, because loading one can run any code inside it.

Journeyman · AI Basics · Python · Updated

By

After this lesson you can

  • Save a trained pipeline with joblib.dump and load it with joblib.load, and check the loaded copy gives the same guesses
  • Explain why a saved model must be the whole pipeline, loaded with the same scikit-learn version, and never loaded from a source you don't trust

Key takeaways

  • A trained model lives in memory and disappears when the notebook closes. joblib.dump writes it to a file, and joblib.load brings it back with exactly the same guesses.
  • Save the whole pipeline, not only the model at its end. A model saved without its scaler gets raw numbers it was never trained on, and its guesses are badly wrong.
  • Load a model with the same scikit-learn version, and the same versions of its other packages, that it was saved with. Pin them in requirements.txt and write the version down beside the file.
  • Loading a joblib or pickle file can run any code hidden inside it. Never load one from someone you don't trust. skops is a safer format that checks what a file holds first.

Pine Ridge Delivery's app needs the model running all day, on a server, not in your notebook. And when When the World Changes says it's time to retrain, the team wants the old model kept, so they can go back to it if the new one does worse. Every model that leaves a notebook, for an app, a teammate or a class project you hand in, has to be saved to a file and loaded again somewhere else.

The example is Pine Ridge Delivery's deliveries.csv, from Exercise: Model Check-Up. Each code block is one notebook cell, run in order.

A model lives in memory

Training a model makes a Python object in the notebook's memory. Close the notebook, or restart its kernel, and the object is gone. Training again every time the app starts is slow on a big dataset, and if the data changed in between, it isn't even the same model anymore. What you want is the trained model in a file, the way a document is saved.

scikit-learn's documentation points to a few ways to save a model. This course uses joblib, which comes with scikit-learn: joblib.dump(model, path) writes the trained object to a file, and joblib.load(path) reads it back. Saving a trained model so it can be used later is called model persistence.

import joblib
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

deliveries = pd.read_csv("deliveries.csv")
features = ["distance_miles", "stops_before", "is_raining"]
train, test = train_test_split(deliveries, test_size=0.2, random_state=42)

model = make_pipeline(StandardScaler(), LinearRegression())
model.fit(train[features], train["minutes"])
print(joblib.dump(model, "delivery_model.joblib"))
['delivery_model.joblib']

joblib.dump gives back a list of the files it wrote: one here, delivery_model.joblib, a small file beside the notebook. The .joblib ending is a habit, not a rule; it tells people what kind of file it is.

Loading it somewhere else

The app's code doesn't train anything. It loads the file and guesses:

loaded = joblib.load("delivery_model.joblib")

order = pd.DataFrame({"distance_miles": [4.2], "stops_before": [2], "is_raining": [1]})
print("Saved model's guess:", round(model.predict(order)[0], 1))
print("Loaded model's guess:", round(loaded.predict(order)[0], 1))
print("Same on all 100 test orders:", (loaded.predict(test[features]) == model.predict(test[features])).all())
Saved model's guess: 32.4
Loaded model's guess: 32.4
Same on all 100 test orders: True

The loaded copy gives exactly the same guess for every test order. That's the check to make every time you save a model: load it back and compare its guesses with the original's on the same rows.

Save the whole pipeline

This model is a pipeline: a StandardScaler that rescales each column, then the linear regression, which learned from the rescaled numbers. Say someone saves only the model at the end, model[-1] (the pipeline's last step), and the app gives it an order:

joblib.dump(model[-1], "line_only.joblib")
line_only = joblib.load("line_only.joblib")
print("Line without its scaler:", round(line_only.predict(order)[0], 1))
Line without its scaler: 70.6

70.6 minutes for the same order the pipeline guessed at 32.4. The line learned its slopes on rescaled numbers and now gets raw miles and stops; scikit-learn also warns X has feature names, but LinearRegression was fitted without feature names, a hint that something is off. scikit-learn's documentation calls this inconsistent preprocessing: every step used in training must be used, the same way, on every new row. Pipelines: Every Step, Every Time put the steps in one object for that reason, and it's the object to save.

Same versions on both sides

A saved model is tied to the packages that made it. scikit-learn's documentation says there's no supported way to load a model saved with a different version of scikit-learn: it might load, but it might give different guesses or crash. When the versions differ, scikit-learn warns with an InconsistentVersionWarning as it loads. Treat that warning as a stop sign.

So keep both sides the same:

  • Pin the versions in the project's requirements.txt (scikit-learn==1.9.1, joblib==1.6.0, and the rest), and install the same file where the model is loaded.
  • Write down what made the file: the scikit-learn version (sklearn.__version__), the training data, the code that trained it, and its test score. With those, you can train a matching model again on newer packages when you have to.

Never load a file you don't trust

joblib files are built on Python's pickle format, and a pickle file can hold instructions that run while it loads. Loading one is like running a program: it can do anything your account can do, from reading your files to deleting them. scikit-learn's and joblib's documentation both say it plainly: never load a pickle or joblib file from a source you don't trust. A model file from a stranger, a forum or an email attachment is a program from a stranger.

When a model must come from somewhere else, there's a safer format. skops is a separate package whose skops.io saves scikit-learn models without pickle, and before it loads a file it lists the types inside that it doesn't already trust, so you can check them first. It's named here so you know it exists; inside your own projects, joblib with files you made yourself is fine.

Check yourself

2 questions to try on the lesson page.

So what

A model is only useful once it can leave the notebook: save the whole pipeline, load it back and compare its guesses, keep the package versions the same on both sides, and load only the files you trust.

Sources

  • scikit-learn 1.9.1, Model persistence: pickle, joblib, cloudpickle, skops and ONNX compared; joblib is more efficient than pickle for models with large NumPy arrays; Security & maintainability limitations: pickle, and joblib and cloudpickle with it, should only be used for files from a trusted and verified source, never an untrusted one; there is no supported way to load a model trained with a different version of scikit-learn, and the results could differ or the process crash; an InconsistentVersionWarning is raised when the versions differ; save the training data, the source code, the versions of scikit-learn and its dependencies and the cross-validation score with the model; skops.io avoids pickle and only loads types that are trusted by default or by the user, listed with get_untrusted_types.
  • joblib, joblib.load: reconstructs an object saved with joblib.dump; it relies on pickle, can execute arbitrary Python code, and should never be used to load files from untrusted sources.
  • joblib, joblib.dump: persists a Python object into one file and returns the list of file names it stored the data in.
  • scikit-learn 1.9.1, Common pitfalls and recommended practices, Inconsistent preprocessing: transformations used in training must also be used on test data and on data in a production system, and a Pipeline makes it harder to forget one.

Key terms

Related