1

This error occurs in my code: AttributeError: 'str' object has no attribute 'mean_validation_score'. What can I do to resolve it?

GridMean = [result.mean_validation_score for result in 
gridA.cv_results_]
print(GridMean)
plt.plot(k_values, GridMean)
plt.xlabel('Value of "K" for KNN')
plt.ylabel('CrossValidated Accuracy')
Nayak
  • 21
  • 3
  • 1
    Obviously the result object is of type string and a string object doesn't have `mean_validation_score` attribute. The code, relevant to fixing this issue is before the part you provided. – kuco 23 Sep 21 '19 at 12:11

2 Answers2

1

"mean_validation_score" is depricated now it's "mean_test_score". Use "mean_test_score".

For your confirmation you can check-it-out

gridA.cv_results_.keys()

After running the above comment you can see there is no "mean_validation_score".

Palash Mondal
  • 468
  • 4
  • 10
-1

I presume you are working with sklearn.model_selection.GridSearchCV, with gridA being an instance of GridSearchCV.

I am not sure when the method mean_validation_score was deprecated for GridSearchCV objects, but for sklearn 0.22 you can get the scores calling them with the key 'mean_test_score' being gridA.cv_results a dictionary. Like this:

GridMean = gridA.cv_results_['mean_test_score']