How To Save & Reload a Python Machine Learning Model using Pickle ?
In this post lets understand How To Save a Machine Learning Model in Python using Pickle ? Pickle is used quite often in Python especially in Machine Learning modelling. We will try to understand what is & How to use Pickle.
What is Pickle
- Pickle is a Standard way of serializing objects in Python. It can be a Machine Learning Algorithm or any other Object.
- You can serialize and save the model or Object using Pickle . It is saved in a serialized format as a file.
- When you need to re-use or re-load the same Model or Object , you can reload and de-serialize the file using Pickle.
Example Code
#======= MODEL BUILDING
import numpy as np
from sklearn import datasets
iris \= datasets.load\_iris()
iris\_X \= iris.data
iris\_y \= iris.target
np.unique(iris\_y)
\# Split iris data in train and test data
\# A random permutation, to split the data randomly
np.random.seed(0)
indices \= np.random.permutation(len(iris\_X))
iris\_X\_train \= iris\_X\[indices\[:\-10\]\]
iris\_y\_train \= iris\_y\[indices\[:\-10\]\]
iris\_X\_test \= iris\_X\[indices\[\-10:\]\]
iris\_y\_test \= iris\_y\[indices\[\-10:\]\]
\# Create and fit a nearest-neighbor classifier
from sklearn.neighbors import KNeighborsClassifier
knn\_model \= KNeighborsClassifier()
knn\_model.fit(iris\_X\_train, iris\_y\_train)
Create a Pickle File
- Our Objecctive is to create a Pickle file of the TRAINED model - knn_model in this case**.** That is we will save the model as a serialized object using Pickle.
- Use the code below -
\# save the knn\_model to disk
filename = 'Our\_Trained\_knn\_model.sav'
pickle.dump(knn\_model, open(filename, 'wb'))
- We have saved our Trained model knn_model as file Our_Trained_knn_model.sav
Re-load the Pickle File
- Our objective is to re-load the Pickle file from disk, De-serialize it and convert it back to our Knn Model to do prediction .
- All these objectives can be achieved with Pickle
- Code is below -
\# load the model from disk
filename = 'Our\_Trained\_knn\_model.sav'
knn\_model\_reloaded = pickle.load(open(filename, 'rb'))
knn\_model\_reloaded.predict(iris\_X\_test)
print(iris\_y\_test)
Other Interesting Reads -
how to use a trained model in python, how to save a classifier in python, joblib vs pickle, joblib save model, saving ml model using joblib, save sklearn model to json, from sklearn.externals import joblib error, python pickle, python pickle dump, how to save a classifier in python, pickle save model, python pickle class, how to use a trained model in python, model.save python, pickle file extension, install pickle python, python pickle vs json, python pickle tutorialspoint, python pickle function, python pickle install, python pickle dump to file example, pickle python stack overflow, python pickle save object, python pickle context manager
python pickle save object,python pickle example, save dictionary python pickle