2

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

def report(grid_scores, n_top=3):
    top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]
    for i, score in enumerate(top_scores):
        print("Rank: {0}".format(i + 1))
        print("Mean validation score: {0:.3f} (std: {1:.3f})".format(
              score.mean_validation_score,
              np.std(score.cv_validation_scores)))
        print("Parameters: {0}".format(score.parameters))
        print("")
report(clf.cv_results_)
DollarAkshay
  • 2,063
  • 1
  • 21
  • 39
user9010401
  • 21
  • 1
  • 5

2 Answers2

3

The error is quite clear: AttributeError: 'str' object has no attribute 'mean_validation_score'

There is only one place you use mean_validation_score and the object you use it on is a string - not what you think it is. string does not support the method you use on it - hence the error:

    for i, score in enumerate(top_scores):                              # score from here
        print("Rank: {0}".format(i + 1))
        print("Mean validation score: {0:.3f} (std: {1:.3f})".format(
              score.mean_validation_score,                              # usage here
              np.std(score.cv_validation_scores)))

Obviosly the top_scores is a iterable of type string - hence when you enumerate it

for i, score in enumerate(top_scores):

it produces indexes i and strings score.

You can resolve it by debugging your code:

top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]

and see why there are strings in it - fix that so it contains the objects that have .mean_validation_score and the error vanishes.


Helpful:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • @Ghost - not sure how I would be able to make this an better answer with your comment and the incomplete code given by the OP 3y prior. Care to elaborate? Whatever you think the OP did - the code and error message pasted point to `score` being a string and not having the method he wants. – Patrick Artner Jul 08 '22 at 08:15
  • Patrick, you are literally not stating anything in your answer, just re-writing the same sentence as he got in the error message. The OP clearly asked hoping that whoever answered knows what the problem is about. Look my answer below, i addressed the issue there. He is using the grid_search method for sklearn RF directly from the documentation, which is a really old and deprecated version (0.15 vs current 1.1.1). You can't make your answer better, because you have no idea what the OP issue is about, you shouldn't have even tried to answer in the first place. – Ghost Jul 08 '22 at 20:02
  • 1
    @ghost I see, Well - people searching for the error on SO will now find both answers. Maybe yours will help 3 years after asking :) – Patrick Artner Jul 09 '22 at 12:18
  • Yeah, i know.. he probably figured it out by now :) – Ghost Jul 10 '22 at 02:10
1

You are probably using the grid_search method from the sklearn 0.15-0.17 version to fit RF args, which was long ago deprecated. The new method for the scores is not even grid_scores_, but cv_results_.

Check this link: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.RandomizedSearchCV.html

And replace the statistic mean_validation_score with the one better for your args sweep.

Or if you want just a complete summary, forget of calling report() just do a pd.DataFrame(random_search.cv_results_), sort by rank_test_score and that's it.

Ghost
  • 1,426
  • 5
  • 19
  • 38