【问题标题】:How to use best estimator from pipeline to predict test set?如何使用管道中的最佳估计器来预测测试集?
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-12-31
      • 2016-07-03
      • 2021-09-23
      • 2016-03-11
      • 2022-09-28
      • 1970-01-01
      • 2020-03-10
      • 2021-12-06
      • 2019-12-21
      相关资源
      最近更新 更多