【问题标题】:GridSearchCV with Scoring Function and Refit Parameter带有评分功能和改装参数的 GridSearchCV
【发布时间】:2020-12-11 11:26:48
【问题描述】:

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

我正在做一个多类多标签分类,为此我定义了自己的记分器。但是,为了拥有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_)

【问题讨论】:

    标签: python scikit-learn xgboost gridsearchcv


    【解决方案1】:

    您应该使用refit="roc_auc_score",即字典中记分员的姓名。来自the docs

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

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

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 1970-01-01
      • 2021-07-11
      • 2023-03-03
      • 2021-02-07
      • 2017-09-08
      • 2021-08-22
      • 1970-01-01
      • 2019-11-16
      相关资源
      最近更新 更多