0

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

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Dibyaranjan Jena
  • 189
  • 1
  • 2
  • 10
  • Possible duplicate: https://stackoverflow.com/questions/10592605/save-classifier-to-disk-in-scikit-learn – ForceBru Oct 12 '20 at 15:29

1 Answers1

1

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

Hamza
  • 5,373
  • 3
  • 28
  • 43