Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Machine Learning. No Comments on Saving and Loading a Machine Learning Model

In this article we will look at saving and loading a machine learning model. We will learn two ways of doing this.

 

Solving a problem using machine learning consists of two steps. The first step is training your model using your training dataset and the second step is to ask your questions to the trained model, that gives the answers.

 

Very often the size of the training dataset is pretty huge because as the dataset size increases, the model becomes more accurate. If we save the model to a file, later on we can use the same model to make the actual prediction so that there is no need to train the model every time whenever we want to ask some questions.

 

Now, we will see how to save our model using Python code. First, we will use pickle module in Python which is used to serialize a Python object into a file. The second way to save the model is by using joblib module. The Python code for saving and loading a machine learning model is as given below.

 

In this, we are using our model which we built using linear regression with single variable.

 

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

df = pd.read_csv("house_prices.csv")
model = linear_model.LinearRegression()
model.fit(df[['Area']], df.Price)
model.predict([[3300]])

import pickle

#We are opening a file named model_pickle in write binary mode
with open('model_pickle', 'wb') as f:
    #Dumping our model into the file referenced as f
    pickle.dump(model, f)

#We are opening a file named model_pickle in read binary mode
with open('model_pickle', 'rb') as f:
    #Loading our model into new_model
    new_model_pickle = pickle.load(f)

new_model_pickle.predict([[3300]])

import joblib

#Dumping the model into a file
joblib.dump(model, 'model_joblib')

new_model_joblib = joblib.load('model_joblib')

new_model_joblib.predict([[3300]])

 

That’s it guys. You have learned how to save and load a machine learning model using pickle and joblib.

 

For further information visit the following URLs:

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

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