Want to Save and Reuse a model later?

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!!!

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 do it:

  1. Save the model as a file
  2. Load the model from the saved file
  3. Use the loaded model to make predictions

Using Pickle to save it as a File:

Import the required packages

You can find the explanation as comment (#).


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.


Using joblib to save it as a File:



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 ❤️.


Leave a Reply

Your email address will not be published. Required fields are marked *