带有评分函数和改装参数的 GridSearchCV [英] GridSearchCV with Scoring Function and Refit Parameter

查看:62
本文介绍了带有评分函数和改装参数的 GridSearchCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题似乎类似于这个,但有那里没有可靠的答案.

My question seems to be similar to this one but there is no solid answer there.

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

I'm doing a multi-class multi-label classification, and for doing that I have defined my own scorers. However, in order to have the refit parameter and get the best parameters of the model at the end we need to introduce one of the scorer functions for the refit. If I do so, I get the error that missing 1 required positional argument: 'y_pred'. y_pred should be the outcome of fit. But not sure where this issue is coming from and how I can solve it.

代码如下:

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_)

推荐答案

你应该使用 refit="roc_auc_score",你的字典中的得分手的名字.来自 文档:

You should use refit="roc_auc_score", the name of the scorer in your dictionary. From the docs:

对于多指标评估,这需要是一个 str 表示将用于找到最佳参数以在最后重新拟合估计器的评分器.

For multiple metric evaluation, this needs to be a str denoting the scorer that would be used to find the best parameters for refitting the estimator at the end.

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

Using a callable for refit has a different purpose: the callable should take the cv_results_ dict and return the best_index_. That explains the error message: sklearn is trying to pass cv_results_ to your auc scorer function, but that function should take parameters y_true and y_pred.

这篇关于带有评分函数和改装参数的 GridSearchCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆