Analytics Vidhya
Published in

Analytics Vidhya

Photo by Olia Gozha on Unsplash

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

import pickle as pkl

You can find the explanation as comments

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!

Like to support? Just click the clap icon as much as you like.

Happy Programming!

--

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Recommended from Medium

General Adversarial Networks (GANS)

Wading through Graph Neural Networks

K-Means Clustering Python Implementation

Algorithm of the Week: Support Vector Machine

Implementing Influence-aware Memory Architectures for Deep Reinforcement Learning with Pytorch

Machine Learning as a Flow: Kubeflow vs. Metaflow

What Is Machine Learning?

Dropout (inverted dropout)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Asha Ponraj

Asha Ponraj

Data Science & Machine Learning Enthusiast | Software Developer | Blogger | https://devskrol.com/ | www.linkedin.com/in/asha-ponraj-a5a76b50

More from Medium

AMAZON RELATED ISSUES IN BRAZIL AND ITS AFFECTS

Tuning Hyperparameters of a Logistic Regression Classifier

The What, Why and How of Data Preprocessing

Should You Major in Social Science?