我正在尝试使用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()开始,我们看到正确的方式提供params in 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')
(注意名称和参数之间的两个下划线)