In machine learning, training a model and testing it is definitely not an end. Should we run this source code of training, tuning everything again to do predictions in future? No Need!!! We can save and reuse the same model later.
There are cool ways to keep the model saved and load it later to use.
There are packages available to save your machine learning model into the file system and to load it whenever you want to reuse it!.
Using Pickle package
Using joblib from sklearn.externals
Steps to Save and Reuse a Model:
- Save the model as a file
- Load the model from the saved file
- Use the loaded model to make predictions
Using Pickle to save it as a File:
Import the required packages
import pickle as pkl
You can find the explanation as comment (#).
filenm = 'LR_AdmissionPrediction.pickle'
#Step 1: Create or open a file with write-binary mode and save the model to it
pickle = pkl.dump(lr, open(filenm, 'wb'))#Step 2: Open the saved file with read-binary mode
lr_pickle = pkl.load(open(filenm, 'rb'))#Step 3: Use the loaded model to make predictions
lr_pickle.predict([[300,85,5,5,5,8,1]])
Output:
array([0.61745881])
In the above code, we created/opened a file with name “LR_AdmissionPrediction.pickle”.
You can find this file in your file system. It will be a very small sized file.
size: This file saves the model not the data you used to train or test the model. So the size of this file does not reflect the size of the data used to train/test the model.
wb,rb : We opened the file in write-binary to create a pickle file and used read-binary to load it later.
Here we have serialized our model and saved it in file system.
To know from where the lr comes from please check Linear Regression — Part IV — Chance of Admission Prediction.
This is not the only way. There is one more way in which u can save it in a string and load it anywhere in the same project where the string’s scope is available. Below is the source code for that.
#Step 1: Save the model as a pickle string.
saved_model = pkl.dumps(lr)
#Step 2: Load the saved model
lr_from_pickle = pkl.loads(saved_model)
#Step 3: Use the loaded model to make predictions
lr_from_pickle.predict([[300,85,5,5,5,8,1]])
Output:
array([0.61745881])
Using joblib to save it as a File:
from sklearn.externals import joblib
#Step 1: Save the model as a pickle in a file
joblib.dump(lr, 'filename.pkl')
#Step 2: Load the model from the file
lr_from_joblib = joblib.load('filename.pkl')
#Step 3: Use the loaded model to make predictions
lr_from_joblib.predict([[300,85,5,5,5,8,1]])
Output:
array([0.61745881])
Conclusion:
In all the above 3 ways, the predicted value is same for same input. That means you can use any method to reuse the model.
If you find any other cool stuffs similar to this or if you find any corrections, I am really grateful to know that, please add it in comments.
Thank you!
Happy Programming!
Like to support? Just click the heart icon ❤️.