【发布时间】:2021-05-26 04:03:19
【问题描述】:
我正在尝试训练 XGBoost 分类器。目标变量y 是二进制的。
数据(找不到样本数据集来完全重现。对此感到抱歉)。
X_train, X_validate, X_test(包含数字和分类数据)
y_train, y_validate, y_test(值为二进制 1/0)。
预处理器。
categorical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='constant', fill_value='MISSING')),
('encoder', OneHotEncoder(handle_unknown='ignore'))])
numerical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='constant', fill_value=-999))])
preprocessor = ColumnTransformer(
remainder='passthrough',
transformers=[
('cat', categorical_transformer, selector(dtype_include="object")),
('num', numerical_transformer, selector(dtype_exclude="object"))
])
型号。
best_clf = Pipeline(steps=[('preprocessor', preprocessor),
('classifier',
xgb.XGBClassifier(
seed=42,
objective='binary:logistic',
missing=-999,
## optimal params
learning_rate = 0.1))])
best_clf.fit(X_train, y_train,
classifier__early_stopping_rounds=10,
classifier__eval_metric='aucpr',
classifier__eval_set=[(X_validate_preprocessed, y_validate)],
classifier__verbose=True)
到目前为止一切正常。我现在有模型。但我想校准这个模型。
校准。
我试过了:
best_clf_calib = Pipeline(steps=[('preprocessor', preprocessor),
('calibrator', CalibratedClassifierCV(
base_estimator=best_clf.named_steps.classifier,
cv='prefit',
method='isotonic'))])
best_clf_calib.fit(X_validate, y_validate)
但它给了我以下错误:
TypeError: predict_proba() got an unexpected keyword argument 'X'
问题:具体应该如何设置CalibratedClassifierCV中的base_estimator参数?我试过设置
base_estimator = best_clf
但在这种情况下,管道似乎运行了两次。这是流水线步骤的示意图。
【问题讨论】:
-
请检查此解决方案是否有效:stackoverflow.com/questions/65517931/…
-
@Anu:是的,降级 sklearn 版本有效。谢谢。如果你把它写下来作为答案,我会接受。
标签: python-3.x machine-learning scikit-learn pipeline xgboost