October 19, 2024

Python’s pickle module is a powerful tool for serializing and deserializing Python objects, but it should be used with caution, especially when dealing with untrusted data sources. Consider other serialization options if you need cross-language compatibility or have strict security requirements.

How to save

from sklearn.lenear_model import LinearRegression
import pickle
model = LinearRegression(fit_intercept=True).fit(X, y)
with open(f"${path}/model.pickle", "wb") as file:
    pickle.dump(model, file)

How to load

import pickle
with open(f"{path}/model.pickle", "rb") as file:
    model2 = pickle.load(file)