python - 如何使用管道中的最佳估计器来预测测试集?
问题描述
我使用 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)
解决方案
经过几次试验,我发现出了什么问题:只需输入:
xgb._best_estimator_.named_steps['xgb'].predict(test_final)
推荐阅读
- json - Json 给出空值
- javascript - Firefox 中的 jQuery - 事件未定义,为什么?
- c++ - 未评估上下文中的默认模板参数和 lambda:错误还是功能?
- alfresco - 使用 Alfresco API 上传新版本的文件
- sql - 三表关系——跨三个ID的唯一性
- google-analytics - 关于何时弃用 Google Analytics(分析)报告 API v3 或 v4 是否有截止日期?
- chronicle - 日志记录器的日志级别设置为“TRACE”导致 AbstractMethodError
- python - 在 docker 容器中调试 python 代码而不需要重建
- javascript - Gatsby,React Native Web - GraphQL 错误预期类型 Craft_SectionsEnum
- c# - 从 Postman 测试时出现错误:HTTP Error 401.2 - Unauthorized