What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Save a fitted scikit-learn estimator with pickle, joblib, or another supported format, then load it in a compatible Python environment. For trusted artifacts that need the Python object restored, joblib is often convenient for large NumPy-heavy models; use skops.io when you want to inspect types before loading, or ONNX when you need prediction serving without Python. The key trade-offs are security, environment compatibility, model size, and whether you need the original Python object.

Save and load a fitted scikit-learn model

Fit the estimator first, then serialize the fitted object. This example uses Python’s standard pickle module:

from pickle import dump, load

# After fitting: model = ...
with open("model.pkl", "wb") as f:
    dump(model, f, protocol=5)

with open("model.pkl", "rb") as f:
    model = load(f)

Use binary file modes: wb when writing and rb when reading. The scikit-learn persistence guide recommends pickle protocol 5 to reduce memory use and speed storage or loading of large NumPy arrays. The file is a serialized fitted object, not a standalone guarantee that the model will work in any Python environment.

Choose a persistence format

Choose based on what you need at load time: the reconstructed Python estimator, safer inspection, efficient handling of large arrays, or inference outside Python.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Format Python object restored? Security and compatibility Useful when
pickle Yes Load only trusted files; loading can execute arbitrary code. Cross-version scikit-learn loading is unsupported. You want a native, broadly capable Python serialization format.
joblib Yes Pickle-based, so the same trusted-source warning applies; environment compatibility still matters. The estimator contains large NumPy arrays, or memory mapping or compression is useful.
cloudpickle Yes Pickle-based and requires compatible dependencies; it has no forward-compatibility guarantee. The model relies on user-defined functions, lambdas, or interactively defined classes that ordinary pickle cannot serialize.
skops.io Yes Inspect unknown types and approve only those you understand. Format and compatibility can vary with releases. You want to inspect types before loading a Python model artifact.
ONNX No Conversion support is incomplete; sandbox artifacts because arbitrary computations and resource-exhaustion risks remain possible. You need inference in a non-Python runtime or a smaller serving environment.

For details, see the scikit-learn model persistence guide, the joblib persistence documentation, and the skops persistence documentation.

Use joblib for NumPy-heavy models

joblib provides a similar save/load workflow to pickle, with features suited to large NumPy arrays:

import joblib

joblib.dump(model, "model.joblib")
model = joblib.load("model.joblib")
# For repeated processes reading large arrays, evaluate mmap_mode="r".

Memory mapping can be useful when multiple processes repeatedly read large arrays, but it is not necessary for every model. Loading with joblib is still capable of arbitrary code execution because joblib uses pickle under the hood. Treat its files as executable content and load them only when their source is trusted.

Use cloudpickle for custom Python objects

When ordinary pickle cannot serialize a user-defined function, lambda, or class defined interactively, cloudpickle may handle it. Its dump/load workflow is conceptually the same as pickle. This flexibility does not make the artifact portable: matching dependencies are still needed, and cloudpickle offers no forward-compatibility guarantee. As with pickle and joblib, load only trusted artifacts.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Inspect a model artifact with skops.io

skops.io lets you inspect unknown types before loading. Review the returned type names and approve only types you recognize and trust:

import skops.io as sio

sio.dump(model, "model.skops")
unknown_types = sio.get_untrusted_types(file="model.skops")
# Review unknown_types, then load only approved types.
model = sio.load("model.skops", trusted=unknown_types)

Do not approve every returned type automatically: verify each one before passing it as trusted. Normal skops loading avoids automatically executing arbitrary code in the way pickle-based loading can, but it supports fewer object types and remains sensitive to the software environment. Pin skops and scikit-learn versions for deployment; the skops documentation notes that format and compatibility can change between releases.

Deploy predictions without Python using ONNX

If a service only needs predictions, consider converting a supported estimator to ONNX and serving it with an appropriate ONNX runtime. The scikit-learn guide describes this option as a way to serve without a Python environment. Not every estimator converts, custom estimators can require extra work, and an ONNX file does not reconstruct the original Python estimator or its custom Python code.

ONNX avoids needing Python for inference, not the need for security controls. Run artifacts in a sandbox: ONNX computations can pose resource-exhaustion risks. Check the scikit-learn persistence guide for conversion and support considerations.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep the model and its environment compatible

A serialized model is coupled to its software environment. The scikit-learn project states that there are no supported ways to load a model trained with a different scikit-learn version; even if cross-version loading appears to work, it is unsupported and inadvisable. Record the versions of Python, scikit-learn, NumPy, SciPy, and the serializer alongside the artifact. Pin the training environment, retain the training code and data references, and test loading in a controlled environment before production use.

Persist a complete Pipeline as one object when preprocessing and prediction steps must stay aligned. For a Python-based format, package the matching environment along with the artifact rather than assuming a file alone captures all its dependencies. The scikit-learn guide and its maintained source documentation explain persistence and compatibility limitations.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.