Dibyaranjan Jena
Dibyaranjan Jena

Reputation: 329

'LinearRegression' object has no attribute 'save'

I am trying to save a Linear model with below lines of code, but I am getting error as 'LinearRegression' object has no attribute 'save'.

from sklearn.linear_model import LinearRegression

model = LinearRegression()

model.save('Linear_Model.h5')

How to fix this issue ?

enter image description here

Upvotes: 0

Views: 2070

Answers (1)

Hamza
Hamza

Reputation: 6025

model.save() is not built for sklearn models as opposed to keras/tensorflow models. You might want to save them as you would do with other python objects as follows:

  1. Save model using pickle

# save the model to disk
filename = 'finalized_model.sav'
pickle.dump(model, open(filename, 'wb'))
 
# some time later...
 
# load the model from disk
loaded_model = pickle.load(open(filename, 'rb'))
result = loaded_model.score(X_test, Y_test)
  1. Save model using joblib:

# save the model to disk
filename = 'finalized_model.sav'
joblib.dump(model, filename)
 
# some time later...
 
# load the model from disk
loaded_model = joblib.load(filename)
result = loaded_model.score(X_test, Y_test)

If you are interested, this article has these codes with examples. Also check the link to the SO question that might already answer what you are looking for

Upvotes: 1

Related Questions