我刚刚开始在Python中使用GridSearchCV,但是我对其中的评分(scoring)感到困惑。我在某些地方看到过
scorers = { 'precision_score': make_scorer(precision_score), 'recall_score': make_scorer(recall_score), 'accuracy_score': make_scorer(accuracy_score)}grid_search = GridSearchCV(clf, param_grid, scoring=scorers, refit=refit_score, cv=skf, return_train_score=True, n_jobs=-1)
使用这些值(即精确度、召回率、准确率)的意图是什么?
GridSearch是否根据这些评分值来为我们提供优化后的参数…比如说,为了获得最佳的精确度评分,它会找到最佳的参数之类的情况?
它会计算可能参数的精确度、召回率和准确率,并给出结果,现在的问题是,如果这是真的,那么它是基于精确度、召回率还是准确率来选择最佳参数的?上述说法是否正确?
回答:
你的假设基本上是正确的。这个参数字典允许网格搜索针对每个评分指标进行优化,并为每个评分找到最佳参数。
然而,如果你没有选择哪个评分用于refit
,你就不能让网格搜索自动拟合并返回best_estimator_
,否则它会抛出以下错误:
ValueError: For multi-metric scoring, the parameter refit must be set to a scorer key to refit an estimator with the best parameter setting on the whole data and makethe best_* attributes available for that metric. If this is not needed, refit should be set to False explicitly. True was passed.