How to save your machine Learning Model Using Pickle and Joblib
Machine learning models are so popular that you find many for a given problem. Most of the time we tend to get code and data from github and many other places. It would be always better if can get trained models downloaded and test with your data.
There are several models which you can use to get this done.
- Use pickle library
- Use Joblib Library
- Use JSON
You can find the same content covered in my youtube channel,
Let's start with pickle,
Let's start the code with a simple LinearRegression code,
import pandas as pd
import numpy as np
from sklearn import linear_model
import pickle
from sklearn.externals import joblib
df = pd.read_csv('homeprices.csv')
print(df.head())
model = linear_model.LinearRegression()
model.fit(df[['area']],df.price)
as you can see in the above code “model” is the trained regression model which we are planning to use in the pickle library. pickle library basically does an object serialization process.
[a short youtube video about pickle]
Next following code module will dump the “model” into a serialising object. I am printing the predicted output as follows before the pickle is tested. In that way, i will be able to test the accuracy of the saved model.
print(model.predict([[5000]]))
Alright now all set to dump and load the pickle,
# now to save the model as serialized object pickle
with open('mysaved_md_pickle', 'wb') as file:
pickle.dump(model,file)
#now we weill load the saved model
with open('mysaved_md_pickle', 'rb') as file:
loaded_model = pickle.load(file)
loaded_model.coef_
loaded_model.intercept_
print(loaded_model.predict([[5000]]))
Now check the two prints. If its the same value, now you know the model works perfectly.
Now for fun check opening the pickle object “mysaved_md_pickle”.
Its all Jibrish. This is binary. Hence don’t try to understand this.
OK. Now its time to do the same from JobLIB,
allright, the code seems simpler and in the same order,
# now will try the same with joblib
joblib.dump(model, 'mysaved_md_jobilb')
# load and test this
whatever = joblib.load('mysaved_md_jobilb')
print(whatever.predict([[5000]]))
OK. That's how you save your ML model. Try IT. Leave a Comment.