gpt4 book ai didi

python - 如何使用 XGboost 针对不同的 `eval_metric` 优化 sklearn 管道?

转载 作者:太空狗 更新时间:2023-10-30 01:12:26 25 4
gpt4 key购买 nike

我正在尝试使用 XGBoost ,并将 eval_metric 优化为 auc(如 here 所述)。

这在直接使用分类器时工作正常,但在我尝试将其用作 pipeline 时失败了.

.fit 参数传递给 sklearn 管道的正确方法是什么?

示例:

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris
from xgboost import XGBClassifier
import xgboost
import sklearn

print('sklearn version: %s' % sklearn.__version__)
print('xgboost version: %s' % xgboost.__version__)

X, y = load_iris(return_X_y=True)

# Without using the pipeline:
xgb = XGBClassifier()
xgb.fit(X, y, eval_metric='auc') # works fine

# Making a pipeline with this classifier and a scaler:
pipe = Pipeline([('scaler', StandardScaler()), ('classifier', XGBClassifier())])

# using the pipeline, but not optimizing for 'auc':
pipe.fit(X, y) # works fine

# however this does not work (even after correcting the underscores):
pipe.fit(X, y, classifier__eval_metric='auc') # fails

错误:
TypeError: before_fit() 得到了一个意外的关键字参数 'classifier__eval_metric'

关于xgboost的版本:
xgboost.__version__ 显示 0.6
pip3 卡住 | grep xgboost 显示 xgboost==0.6a2

最佳答案

错误是因为在管道中使用时,您在估算器名称及其参数之间使用了单个下划线。应该是两个下划线。

来自documentation of Pipeline.fit() ,我们看到在 fit 中提供参数的正确方法:

Parameters passed to the fit method of each step, where each parameter name is prefixed such that parameter p for step s has key s__p.

所以在你的情况下,正确的用法是:

pipe.fit(X_train, y_train, classifier__eval_metric='auc')

(注意名称和参数之间的两个下划线)

关于python - 如何使用 XGboost 针对不同的 `eval_metric` 优化 sklearn 管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42793709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com