如何修复 OneClassSVM 和 GridSearchCV 的错误 "For multi-metric scoring"

How to fix the error "For multi-metric scoring" for OneClassSVM and GridSearchCV

我正在尝试使用 OnClassSVM 进行异常检测,我使用 GridSearchCV() 调整了它的参数,如下所示:

我已经在很多网站上搜索过它,包括 https://whosebug.com/,但找不到任何适合我的场景的解决方案。 代码在这里:

nus = [0.001, 0.01, 0.1, 1]
gammas = [0.001, 0.01, 0.1, 1]
scorers = {
  'precision_score': make_scorer(precision_score),
  'recall_score': make_scorer(recall_score),
 'accuracy_score': make_scorer(accuracy_score)
}

tuned_parameters = {'C': [1, 10, 100, 1000], 'kernel' : ['rbf','linear'], 
'gamma' : gammas, 'nu': nus}
tuned_ocsvm = svm.OneClassSVM()
ocsvm = GridSearchCV(estimator=svm.OneClassSVM(), 
param_grid=tuned_parameters, scoring=scorers,refit='false')

但它给我的错误如下

For multi-metric scoring, the parameter refit must be set to a scorer key or a callable to refit an estimator with the best parameter setting on the whole data and make the best_* attributes available for that metric. If this is not needed, refit should be set to False explicitly. 'false' was passed

GridSearchCV's doc 上,refit 定义为:

refit : boolean, string, or callable, default=True

Refit an estimator using the best found parameters on the whole dataset. For multiple metric evaluation, this needs to be a string denoting the scorer that would be used to find the best parameters for refitting the estimator at the end. Where there are considerations other than maximum score in choosing a best estimator, refit can be set to a function which returns the selected best_index_ given cv_results_. The refitted estimator is made available at the best_estimator_ attribute and permits using predict directly on this GridSearchCV instance. Also for multiple metric evaluation, the attributes best_index_, best_score_ and best_params_ will only be available if refit is set and all of them will be determined w.r.t this specific scorer. best_score_ is not returned if refit is callable. See scoring parameter to know more about multiple metric evaluation.

如果不想重新拟合估算器,可以设置refit=False(布尔值)。另一方面,要用其中一个记分器重新拟合估计器,您可以执行 refit='precision_score' 例如。

最好将您的 scorers 设置为记分员之一,不要将其作为字典传递。

scorers = make_scorer(precision_score)

如果你设置 refit=False 那么你将失去 best_estimator_ 这是网格搜索最重要的输出之一。