如何在 GridSearchCV 中正确配置多个评估指标

13次阅读

如何在 GridSearchCV 中正确配置多个评估指标

gridsearchcv 的 `scoring` 参数不接受集合(set)类型,必须使用列表、元组、字典或字符串;将 `{‘precision’,’f1′,’recall’,’accuracy’}` 改为 `[‘precision’, ‘f1’, ‘recall’, ‘accuracy’]` 即可解决 invalidparametererror。

在使用 GridSearchCV 进行超参数调优时,若希望同时监控多个模型性能指标(如准确率、精确率、召回率和 F1 分数),需确保 scoring 参数的类型符合 scikit-learn 的严格要求。常见错误是误用花括号 {} 创建集合(set),例如:

# ❌ 错误:set 类型不被支持 grid = GridSearchCV(estimator, param_grid, scoring={'precision', 'f1', 'recall', 'accuracy'})

该写法会触发 InvalidParameterError,因为 scoring 不接受 set —— 尽管其元素合法,但类型非法。

✅ 正确做法是使用列表(list)(最简洁常用):

from sklearn.model_selection import GridSearchCV from sklearn.ensemble import RandomforestClassifier from sklearn.datasets import make_classification  X, y = make_classification(n_samples=1000, n_features=10, n_classes=2, random_state=42) clf = RandomForestClassifier(random_state=42) param_grid = {'n_estimators': [50, 100], 'max_depth': [3, 5]}  # ✅ 正确:传入字符串列表,支持多指标 grid = GridSearchCV(     estimator=clf,     param_grid=param_grid,     scoring=['accuracy', 'precision', 'recall', 'f1'],  # 注意:方括号,非花括号     cv=5,     refit='f1'  # 必须指定一个主优化指标用于最终模型选择 )  grid.fit(X, y)

⚠️ 关键注意事项:

  • 当使用多指标列表时,refit 参数必须显式指定(如 ‘f1’、’precision’ 等),否则会报错:ValueError: For multi-metric scoring, the parameter refit must be set to a scorer key.
  • 返回的 grid.cv_results_ 将包含每个指标的详细结果,键名为 ‘mean_test_‘(如 ‘mean_test_f1’, ‘mean_test_accuracy’)。
  • 若需自定义组合逻辑(如加权平均),可传入字典形式:
scoring = {     'acc': 'accuracy',     'prec_macro': 'precision_macro',     'rec_macro': 'recall_macro',     'f1_macro': 'f1_macro' } grid = GridSearchCV(..., scoring=scoring, refit='f1_macro')

或更灵活地传入可调用对象(callable),返回指标名与分数的字典:

from sklearn.metrics import accuracy_score, f1_score  def custom_scorer(estimator, X, y):     y_pred = estimator.predict(X)     return {         'accuracy': accuracy_score(y, y_pred),         'f1_weighted': f1_score(y, y_pred, average='weighted')     }  grid = GridSearchCV(..., scoring=custom_scorer, refit='f1_weighted')

总结:scoring 的合法类型仅有 str、list、tuple、dict 或 callable;杜绝使用 set。优先选用列表形式实现多指标评估,并务必通过 refit 明确指定主优化目标,才能让 GridSearchCV 正常运行并返回可解释的结果。

text=ZqhQzanResources