多个模型的 GridSearchCV

GridSearchCV for multiple models

我正在尝试创建一个需要多个模型的 GridSearch CV 函数。但是,出现以下错误:TypeError: not all arguments converted during string formatting

def grid(model, X_train,y_train):
    grid_search = GridSearchCV(model, parameters, cv=5)
    grid_search.fit(X_train, y_train)
    prediction = grid_search.predict(X_test)
    best_classifier = grid_search.best_estimator_

    return grid_search

clf = [('DecisionTree',DT()),('RandomForest',RF())

n_folds = 15

for model in clf:
    
    print('\nWorking on ', model[0])
    
    grid_search = grid(model,X_train,y_train)

您已将模型存储在元组列表中(请注意,在您的示例中实际上缺少右括号):

clf = [('DecisionTree', DT()), ('RandomForest', RF())]

由于您遍历所有元组并且您的实际模型存储在每个元组的索引 1 中,因此您必须将 model[1] 传递给您的函数:

for model in clf:
    print('\nWorking on ', model[0])
    grid_search = grid(model[1], X_train, y_train) # <-- change in this line