Skip to content
Advertisement

How to use pickle to save sklearn model

I want to dump and load my Sklearn trained model using Pickle. How to do that?

Advertisement

Answer

Save:

import pickle

with open("model.pkl", "wb") as f:
    pickle.dump(model, f)

Load:

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

In the specific case of scikit-learn, it may be better to use joblib’s replacement of pickle (dump & load), which is more efficient on objects that carry large numpy arrays internally as is often the case for fitted scikit-learn estimators:

Save:

import joblib

joblib.dump(model, "model.joblib")

Load:

model = joblib.load("model.joblib")
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement