Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 40 ยท Saving and Using Models

Saving and Loading Models with joblib

Persist trained scikit-learn pipelines with joblib and pickle, avoid version and security pitfalls, store metadata, and learn when to use skops or ONNX instead.

Training can take minutes or hours; predicting takes milliseconds. You do not want to retrain every time a web service starts or a nightly job runs. So you persist the fitted model: write it to a file after training, load it where you need predictions.

In scikit-learn the standard tool is joblib, a library that serializes Python objects and is especially efficient with large NumPy arrays. This lesson shows how to save and load, what must be kept together, and the traps: library versions and unsafe files.

The in-browser runner has no persistent disk. The examples below save to memory (a BytesIO buffer) so you can run them here; on your own computer you would use a file name such as model.joblib instead.

Save a whole pipeline

Always persist the entire pipeline: preprocessing and model together. If you save only the classifier, you must recreate the exact same scaling and encoding at prediction time, and any mismatch silently produces wrong results.

Python Dump and load a pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The loaded object is a fully working pipeline. In a different process you would just do model = joblib.load("model.joblib") once at startup and call model.predict(...) for each request.

Compression and size

Large models (random forests with many deep trees) can be hundreds of MB. joblib.dump(model, path, compress=3) trades save/load time for a smaller file (levels 0-9; 3 is a good default).

Python Compression versus size

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You can also shrink a forest by limiting max_depth, n_estimators and min_samples_leaf, or by using a more compact model, which also speeds up prediction.

Save more than the model: metadata

A model file alone does not tell you which data, features or version produced it. Store a small dictionary next to it: scikit-learn version, feature names and order, training date, metrics and the target definition.

Python A model bundle with metadata

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Using a DataFrame with feature_names_in_ lets scikit-learn warn you when column names or order differ at prediction time.

Pickle versus joblib

Python's own pickle module works too; joblib is the recommended choice for scikit-learn because it stores big NumPy arrays more efficiently. Both share the same dangers:

Persistence traps

Versions Load with the same scikit-learn version.
Security Loading can run arbitrary code.
Custom code Your classes must be importable.

A model file is a program, not just data.

1 / 2
  1. Version compatibility. A model saved with one scikit-learn version may fail or behave differently under another. Pin the version in the environment that serves predictions, and store it with the model. scikit-learn warns (InconsistentVersionWarning) when it notices a mismatch.
  2. Security. Unpickling can execute arbitrary code. Only load files you created or fully trust. Never load models uploaded by users.
  3. Custom classes. A pipeline containing your own custom transformer can only be loaded where that class can be imported under the same module path. Keep it in a package, not in a notebook cell or __main__.

Alternatives

Option Good for Notes
joblib / pickle Same Python environment Fast, simplest; version-sensitive
skops.io Safer sharing of scikit-learn models Lets you inspect and whitelist unknown types before loading
ONNX (skl2onnx) Serving from other languages or runtimes Converts supported estimators; not every custom step converts
Model parameters as JSON Tiny linear models You rewrite prediction code yourself
PMML Legacy enterprise systems Limited coverage

For a linear model, the "model" is just numbers, so exporting coefficients is easy and transparent.

Python Exporting a linear model as plain JSON

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A deployment checklist

  1. Save the full pipeline plus metadata (version, features, date, metrics).
  2. Load it once at service start, not per request.
  3. Validate incoming data: same columns, sensible ranges.
  4. Test a stored sample of inputs and expected outputs after every deployment.
  5. Retrain on a schedule and keep old model files so you can roll back.

Serving details and monitoring are covered next in inspecting models and calibration and thresholds.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should you save the entire Pipeline rather than just the final classifier?

    The fitted scaler, encoder and imputer are part of the model. Saving everything together removes mismatch risk.
  2. 2. What is the main security risk of loading a pickled or joblib file?

    Unpickling may execute code embedded in the file, so only load trusted files.
  3. 3. What is a common cause of errors when loading a model months later?

    Internal class layouts change between versions. Record and pin the version used for training.
  4. 4. Which option lets you serve a scikit-learn model from a non-Python runtime?

    ONNX is a portable model format with runtimes in many languages.

Summary

  • Persist the whole pipeline with joblib.dump and joblib.load; use compress= to shrink files.
  • Store metadata (versions, feature order, metrics) with the model.
  • Model files are not safe to load from untrusted sources and are tied to library versions.
  • Consider skops, ONNX or plain JSON exports for safer or cross-language use.

Next: Inspecting Models