【发布时间】:2019-10-30 03:07:05
【问题描述】:
我使用 XGBoost 开发了一个管道,它为我提供了最佳估算器。 但是,尝试使用这个最佳估计器来预测我的测试集时,会出现以下错误:“ValueError: Specifying the columns using strings is only supported for 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)
【问题讨论】:
标签: python python-3.x machine-learning pipeline xgboost