python 11 lines · 1 tab

Serializing models with joblib, pickle, and ONNX tradeoffs

1 tab
import joblib
from skl2onnx import to_onnx
from skl2onnx.common.data_types import FloatTensorType

joblib.dump(model, 'artifacts/model.joblib')

initial_types = [('features', FloatTensorType([None, X_train.shape[1]]))]
onnx_model = to_onnx(model, initial_types=initial_types, target_opset=17)

with open('artifacts/model.onnx', 'wb') as file_handle:
    file_handle.write(onnx_model.SerializeToString())
1 file · python Explain with highlit

Model serialization is not just a file-format choice. It affects startup time, compatibility, portability, and security boundaries. I use joblib for common scikit-learn pipelines, reserve pickle for trusted internal workflows, and reach for ONNX when I need cross-runtime portability.

Share this code

Here's the card — post it anywhere.

Serializing models with joblib, pickle, and ONNX tradeoffs — share card
Link copied