Skip to main content
 首页 » 编程设计

python之如何从 GridSearchCV 中的每个折叠中获得准确性和 f1 分数

2025年04月02日14leader

我正在使用 GridSearchCV 对象来训练分类器。我设置了 5 折验证参数搜索,在调用 fit() 之后,我需要查看每个折验证集的指标,即准确性和 f1 分数。我该怎么做?

 clf = GridSearchCV(pipeline, 
                        param_grid=param_grid,  
                        n_jobs=1,  
                        cv=5, 
                        compute_training_score=True) 

注意:

  • 我没有单独的测试集可以使用,所以我不能只获取预测结果并使用标准度量函数来完成。
  • 使用 clf.best_scores_ 无法提供我想要的信息,只能提供 mean_validation_score 及其标准差。

请您参考如下方法:

分数位于 grid_scores_ 中,特别是在 cv_validation_scores 中:

grid_scores_ : list of named tuples

Contains scores for all parameter combinations in param_grid. Each entry corresponds to one parameter setting. Each named tuple has the attributes:

  • parameters, a dict of parameter settings
  • mean_validation_score, the mean score over the cross-validation folds
  • cv_validation_scores, the list of scores for each fold

但是您不会获得两个指标。这种优化器的全部意义在于最大化一些单一的度量/记分函数,因此只有这个东西存储在一个对象中。为了得到这样的结果,您需要运行它两次,每次使用不同的评分函数。