user12967240
user12967240

Reputation:

How to fix valueError: too many values to unpack (expected 3)

I've tried to run code and get the error too many values to unpack, how to fix the issue? This a piece of code, cannot see the error...

parameter_grid = [{'n_estimators': [100], 'max_depth': [2, 4, 7, 12, 16]}, {'max_depth': [4], 'n_estimators': [25, 50, 100, 250]}]

metrics = ['precision_weighted', 'recall_weighted']

for metric in metrics:
    print("\n##### searching optimal parameters for ", metric)
    classifier = GridSearchCV(ExtraTreesClassifier(random_state=0), parameter_grid, cv=5, scoring=metric)
    classifier.fit(X_train, y_train)

    print("\ngrid scores for the parameter grid:")
    for params, avg_score, _ in classifier.cv_results_:
        print(params, '-->', round(avg_score, 3))

Upvotes: 2

Views: 1671

Answers (1)

Yahya
Yahya

Reputation: 14102

cv_results_ is a dict that contains the results of the GridSearchCV. It looks something like this:

enter image description here

That means if you want to access it, you need to access it the same way you access a dictionary in Python:

for key, value in classifier.cv_results_.items():
    # do something like print("{} ... {}".format(key, value))

The error basically says: You are asking to unpack the content, which contains too many values, into few placeholders/receivers that are less than what we can unpack!

Upvotes: 1

Related Questions