Chapter 21. Saving and Loading Trained Models
21.0 Introduction
In the last 20 chapters and around 200 recipes, we have covered how to take raw data and use machine learning to create well-performing predictive models. However, for all our work to be worthwhile we eventually need to do something with our model, such as integrating it with an existing software application. To accomplish this goal, we need to be able to both save our models after training and load them when they are needed by an application. That is the focus of our final chapter together.
21.1 Saving and Loading a scikit-learn Model
Problem
You have a trained scikit-learn model and want to save it and load it elsewhere.
Solution
Save the model as a pickle file:
# Load libraries
from
sklearn.ensemble
import
RandomForestClassifier
from
sklearn
import
datasets
from
sklearn.externals
import
joblib
# Load data
iris
=
datasets
.
load_iris
()
features
=
iris
.
data
target
=
iris
.
target
# Create decision tree classifer object
classifer
=
RandomForestClassifier
()
# Train model
model
=
classifer
.
fit
(
features
,
target
)
# Save model as pickle file
joblib
.
dump
(
model
,
"model.pkl"
)
['model.pkl']
Once the model is saved we can use scikit-learn in our destination application (e.g., web application) to load the model:
# Load model from file
classifer
=
joblib
.
load
(
"model.pkl"
)
And use it make predictions:
# Create new observation
new_observation
=
[[
5.2
,
3.2
,
1.1
,
0.1
]]
# Predict observation's class
classifer
.
predict
(
new_observation ...
Get Machine Learning with Python Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.