How to save a model in machine learning?

Why we need to save the model?

As we saw in the previous post when executed for the second time the accuracy level dropped from 89% to 81%. So, it is necessary to save the model with maximum accuracy.

How to save the model?

To save the model we use the python module called pickle.

First we create a pickle file using dump() function of pickle. It takes two arguments i.e the model and the name of the file.
 
            with open('studentmodel.pickle','wb') as f:
                        pickle.dump(model, f)
In order to use the file, we first open it and load it using the load() function of pickle. Load function takes the path of the pickle file as an argument.

            
pickle_in = open('studentmodel.pickle','rb')
            model = pickle.load(pickle_in)


Code:

import numpy as np
import pandas as pd
import sklearn
from sklearn import linear_model
from sklearn.utils import shuffle
import pickle

data = pd.read_csv("student-mat.csv",sep=';')

data = data[['G1','G2','G3','studytime','failures','absences']]

print(data.head())

predict = "G3"

X = np.array(data.drop([predict],1))
y = np.array(data[predict])

best = 0
for _ in range(30):
    x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X,y,test_size=0.1)

    model = linear_model.LinearRegression()

    model.fit(x_train,y_train)

    accuracy = model.score(x_test,y_test)
    print("Accuraccy {}".format(accuracy))

    if accuracy > best:
        best = accuracy
        with open('studentmodel.pickle','wb'as f:
            pickle.dump(model, f)

pickle_in = open('studentmodel.pickle','rb')
model = pickle.load(pickle_in)

print("Coefficient :",model.coef_)
print("Intercept :",model.intercept_)

predictions = model.predict(x_test)

for x in range(len(predictions)):
    print(predictions[x],x_test[x],y_test[x])





Post a Comment