Saving Machine Learning Models by joblib

Goal

This post aims to introduce how to save the machine learning model using joblib.

Libraries

In [4]:
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_boston
import joblib

Create a data

In [5]:
boston = load_boston()
X, y = boston.data, boston.target

Train a model

In [6]:
reg = LinearRegression()
reg.fit(X, y)
Out[6]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
         normalize=False)

Save the model as pickle

In [7]:
joblib.dump(reg, 'linear_regression.pkl')
Out[7]:
['linear_regression.pkl']

check the saved model

In [11]:
!ls | grep linear*pkl
linear_regression.pkl

Load the saved model

In [13]:
loaded_reg = joblib.load('linear_regression.pkl')
loaded_reg
Out[13]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
         normalize=False)

Comments

Comments powered by Disqus