我正在尝试在 GridSearchCV 中使用多个指标。我的项目需要多个指标,包括“准确性”和“f1 分数”。但是,在遵循 sklearn 模型和在线帖子之后,我似乎无法让我的工作。这是我的代码:
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import f1_score
clf = KNeighborsClassifier()
param_grid = {'n_neighbors': range(1,30), 'algorithm': ['auto','ball_tree','kd_tree', 'brute'], 'weights': ['uniform', 'distance'],'p': range(1,5)}
#Metrics for Evualation:
met_grid= ['accuracy', 'f1'] #The metric codes from sklearn
custom_knn = GridSearchCV(clf, param_grid, scoring=met_grid, refit='accuracy', return_train_score=True)
custom_knn.fit(X_train, y_train)
y_pred = custom_knn.predict(X_test)
我的错误发生在custom_knn.fit(X_train,y_train)
. 此外,如果您注释掉scoring=met_grid, refit='accuracy', return_train_score=True
,它可以工作。这是我的错误:
ValueError: Target is multiclass but average='binary'. Please choose another average setting.
此外,如果您能解释多个指标评估或将我推荐给可以的人,那将不胜感激!
谢谢