How to save only the model with best accuracy and use it for further use in python . I have added the code below . As i get a model with 0.81 acuracy

Viewed 41

I want to save the model with best accuracy out of all the model accuracies got

best = 0
for _ in range(50):
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1)


#linear = linear_model.LinearRegression()

rfr.fit(x_train, y_train)
acc = rfr.score(x_test, y_test)
print("Accuracy: " + str(acc))

if acc > best:
    best = acc
    with open("qsar1.pickle", "wb") as f:
        pickle.dump(rfr, f)

Output I observe is:

Accuracy: 0.4726128227255264
Accuracy: 0.3888391608617947
Accuracy: 0.6380193495408448
Accuracy: 0.5291825621844575
Accuracy: 0.662572071612386
Accuracy: 0.6714049999599392
Accuracy: 0.7081626761401754
Accuracy: 0.5548099175871721
Accuracy: 0.7402833223157026
Accuracy: 0.655768653935897
Accuracy: 0.4831562741776121
Accuracy: 0.6898065101906802
Accuracy: 0.5268832148437612
Accuracy: 0.5636892561544907
Accuracy: 0.818236386004686
Accuracy: 0.6332857409901576
Accuracy: 0.7591992333641555
Accuracy: 0.5066293109792837
Accuracy: 0.6422434516082971

Any help?

1 Answers

This will do what you want I think, save only the best model. However, this code is strange because you don't test differents model but 50 times the same model with different spliting. For linear regression you should only cut data once to have a confident accuracy metric or you should use cross validation you have the documentation here.

best = 0 
best_model = None
for _ in range(50): 
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1)
  rfr = linear_model.LinearRegression()

  rfr.fit(x_train, y_train)
  acc = rfr.score(x_test, y_test)
  print("Accuracy: " + str(acc))

  if acc > best:
      best = acc
      best_model = rfr
      
with open("qsar1.pickle", "wb") as f:
    pickle.dump(rfr, f)
Related