Pickle is a Python module used for serialization and deserialization of objects in Python. Its ability to save the object in the binary file has been very popular among data analysts and machine learning engineers. It can save the trained machine learning model in a binary Pickle file with the extension of .pkl. In this article, we will not focus on saving the model into a binary file, which you can check in the article about saving the model in the Pickle module. Our main goal is to learn how to load saved model in Pickle and use it to make predictions.
How to Load Saved Model in Pickle?
For this tutorial, we already assume that you have a saved model with the extension .pkl or without the extension just a binary file. The pickle.load() method is used to load saved model in Python. This method opens the saved model in a read binary mode which helps the binary file to be read.
Here is the simple syntax of the function load() to load saved model.
model = pickle.load(open('file.pkl', 'rb'))
- Pickle: Pickle is the module
- load(): method to open the file
- file.pkl: It is the name of the saved model
- rb: Read in binary mode
Before going to open the saved model in Pickle, make sure that you have installed the Pickle module on your system. Use the pip command to install the module on your system in case you have not installed it yet.
pip install pickle
Example of Loading a Saved Model in Python
Let us now take a simple example and see how we can use the Pickle to open a saved model and then use this model to make predictions. We assume that you also have a model saved in the binary file and you want to open the file in the Pickle module.
Let us first import the pickle module.
Now, you can load the saved model using the load() method. We have already saved an XGboost model which we will open here.
# load pickle model model = pickle.load(open('XGBoost_reg', 'rb'))
Now you have successfully loaded the model. You can use this model to make predictions.
# making predictions model.predict(x_test)
Congratulations on making predictions using the saved model in Pickle.
How to Evaluate the Performance of a Saved Model?
Finding the accuracy or the performance of the saved model is very similar to finding another model. All you need to open the model and then apply the same functions. We have already opened the model and we will use it and find its performance. One thing to note here is that you need to know whether the model is a classification model or a regression model. The reason is that, for each of them, there are different evaluation matrices.
Here we will use the classification model and we will use it to find the accuracy and confusion matrix. First, we will make predictions on the testing data and will save the predictions in a variable:
# making predictions y_pred = model.predict(x_test)
Let us first create the confusion matrix to see the actual and predicted values.
# importing modules import matplotlib.pyplot as plt from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay #confusion matrix cm = confusion_matrix(y_test, y_pred, labels=model.classes_) # labelling disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model.classes_) disp.plot() plt.show()
Now, we can use the same predictions and find the accuracy of the model.
from sklearn.metrics import classification_report print(classification_report(y_test, y_pred))
precision recall f1-score support 0 1.00 1.00 1.00 3217 1 1.00 1.00 1.00 114 2 1.00 1.00 1.00 4408 3 1.00 1.00 1.00 1002 4 1.00 1.00 1.00 1005 5 0.99 1.00 0.99 250 accuracy 1.00 9996 macro avg 1.00 1.00 1.00 9996 weighted avg 1.00 1.00 1.00 9996
As you can see, we have calculated the classification report of the model.
Pickle is commonly used to save and load the machine learning model. The dump() method is used to save the model and the load() method is used to open the model. In this post, we discussed how to load a saved model in Pickle.