首页 > 解决方案 > 如何使用管道中的最佳估计器来预测测试集?

问题描述

我使用 XGBoost 开发了一个管道,它返回了我最好的估计器。但是,尝试使用这个最佳估计器来预测我的测试集会引发以下错误:“ValueError:仅支持 pandas DataFrames 使用字符串指定列”。

下面是我使用的管道代码: 注意:ct 只是 ColumnTransformer,使用 SimpleImputer 和 OneHotEncoder 用于分类列,SimpleImputer 和 StandardScaler 用于数字列

ml_step_1 = ('transform', ct)
ml_step_2 = ('pca', PCA())
xgb = ('xgb', XGBRegressor())
xgb_pipe = Pipeline([ml_step_1, ml_step_2, xgb])
xgb = RandomizedSearchCV(xgb_pipe, xgb_param_grid, cv=kf, scoring='neg_mean_absolute_error');
xgb.fit(train_full_features, train_full_target);

运行以下管道,这是我得到的最佳估算器:

Best XGBoost parameters: {'xgb__silent': True, 'xgb__n_estimators': 1000, 'xgb__max_depth': 4, 'xgb__learning_rate': 0.09999999999999999, 'transform__num__imputer__strategy': 'median', 'transform__cat__imputer__strategy': 'most_frequent', 'pca__n_components': 68}

现在,我调用了这个最好的估计器并做了以下事情:

test_full_imp = pd.DataFrame(xgb.best_estimator_.named_steps['transform'].transform(test_full))
test_final = xgb.best_estimator_.named_steps['pca'].transform(test_full_imp)
predictions = xgb.best_estimator_.predict(test_final)

标签: pythonpython-3.xmachine-learningpipelinexgboost

解决方案


经过几次试验,我发现出了什么问题:只需输入:

xgb._best_estimator_.named_steps['xgb'].predict(test_final)

推荐阅读