Let's say you have a trained model named your_model, and you want to save it instead of training it every time you need to use the model. You want to reuse the model.

from sklearn.linear_model import LinearRegression
your_model = LinearRegression()

# Let's pretend that `your_model` was trained. Shall we? ;)

For that, we can use joblib or pickle, and thanks to the Python community, both of them are included if you use Anaconda. So you don't need to install any of them for saving your models. Let's get into it.

Saving Models

joblib

from joblib import dump
dump(your_model, 'your_model.joblib')
['your_model.joblib']

pickle

import pickle
with open('your_model_2.pickle', 'wb') as f:
    f.write(pickle.dumps(your_model))

Loading Models

joblib

from joblib import load
saved_model = load('your_model.joblib')

pickle

import pickle
saved_model_2 = pickle.loads(open('your_model_2.pickle', 'rb').read())
saved_model # to check your `saved_model` is loaded correctly or not
LinearRegression()
saved_model_2 # to check your `saved_model_2` is loaded correctly or not
LinearRegression()