简体   繁体   中英

'too many values to unpack', while iterating over a for loop

I'm iterating over multiple regression models in a prediction algorithm. While I iterate over the models from a single list, there is no such error. But when I iterate over 2 lists, one for the models(' classifiers '), and the other for the name of the models as strings( 'ListOfclassifiers' ), I get an error like: 'ValueError: too many values to unpack (expected 2)'

ListOfclassifiers = ['svm.SVR', 'SGDRegressor', 'BayesianRidge', 'LassoLars', 'ARDRegression',
                     'PassiveAggressiveRegressor',
                     'TheilSenRegressor', 'LinearRegression']
classifiers = [svm.SVR(),
               linear_model.SGDRegressor(),
               linear_model.BayesianRidge(),
               linear_model.LassoLars(),
               linear_model.ARDRegression(),
               linear_model.PassiveAggressiveRegressor(),
               linear_model.TheilSenRegressor(),
               linear_model.LinearRegression()]

for items, types in classifiers, ListOfclassifiers:
    clf = items
    clf.fit(X_train, y_train)
    clf.score(X_test, y_test)
    accuracy = clf.score(X_test, y_test)
    print('The accuracy with', types, 'is:', accuracy)

Merge two arrays with zip method:

for items, types in zip(classifiers, ListOfclassifiers):
    # todo smth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM