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 librariesfromsklearn.ensembleimportRandomForestClassifierfromsklearnimportdatasetsfromsklearn.externalsimportjoblib# Load datairis=datasets.load_iris()features=iris.datatarget=iris.target# Create decision tree classifer objectclassifer=RandomForestClassifier()# Train modelmodel=classifer.fit(features,target)# Save model as pickle filejoblib.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 fileclassifer=joblib.load("model.pkl")
And use it make predictions:
# Create new observationnew_observation=[[5.2,3.2,1.1,0.1]]# Predict observation's classclassifer.predict(new_observation ...