具有评分函数和改装参数的 GridSearchCV

Soy*_*yol 1 python scikit-learn xgboost gridsearchcv

我的问题似乎与类似,但没有可靠的答案。

我正在进行多类多标签分类,为此我定义了自己的评分器。然而,为了refit最终获得参数并获得模型的最佳参数,我们需要引入评分器函数之一进行改装。如果我这样做,我会收到错误消息missing 1 required positional argument: 'y_pred'。y_pred 应该是拟合的结果。但不确定这个问题从何而来以及如何解决。

下面是代码:

scoring = {'roc_auc_score':make_scorer(roc_auc_score),
          'precision_score':make_scorer(precision_score, average='samples'),
          'recall_score':make_scorer(recall_score, average='samples')}

params = {'estimator__n_estimators': [500,800],
          'estimator__max_depth': [10,50],}

model = xgb.XGBClassifier(n_jobs=4)
model = MultiOutputClassifier(model)

cls = GridSearchCV(model, params, cv=3, refit=make_scorer(roc_auc_score), scoring = scoring, verbose=3, n_jobs= -1)

model = cls.fit(x_train_ups, y_train_ups)
print(model.best_params_)
Run Code Online (Sandbox Code Playgroud)

Ben*_*ger 5

您应该使用refit="roc_auc_score"字典中记分员的名字。来自文档

对于多个指标评估,这需要表示str评分器,用于找到最终重新拟合估计器的最佳参数。

使用可调用对象refit有不同的目的:可调用对象应该采用cv_results_dict 并返回best_index_. 这解释了错误消息:sklearn 正在尝试传递cv_results_给您的 auc 评分器函数,但该函数应该采用参数y_truey_pred