Title: Saving Machine Learning Models
Slug: saving_machine_learning_models
Summary: Saving Machine Learning Models from scikit learn.
Date: 2016-09-22 12:00
Category: Machine Learning
Tags: Basics
Authors: Chris Albon
In scikit there are two main ways to save a model for future use: a pickle string and a pickled model as a file.
In [1]:
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
import pickle
from sklearn.externals import joblib
In [2]:
# Load the iris data
iris = datasets.load_iris()
# Create a matrix, X, of features and a vector, y.
X, y = iris.data, iris.target
In [3]:
# Train a naive logistic regression model
clf = LogisticRegression(random_state=0)
clf.fit(X, y)
Out[3]:
In [4]:
# Save the trained model as a pickle string.
saved_model = pickle.dumps(clf)
In [5]:
# View the pickled model
saved_model
Out[5]:
In [6]:
# Load the pickled model
clf_from_pickle = pickle.loads(saved_model)
# Use the loaded pickled model to make predictions
clf_from_pickle.predict(X)
Out[6]:
In [7]:
# Save the model as a pickle in a file
joblib.dump(clf, 'filename.pkl')
Out[7]:
In [8]:
# Load the model from the file
clf_from_joblib = joblib.load('filename.pkl')
In [9]:
# Use the loaded model to make predictions
clf_from_joblib.predict(X)
Out[9]: