开始使用免费开始使用

使用 XGBoost 进行网格搜索

既然您已经学会了如何在 XGBoost 中单独调参,接下来我们通过 scikit-learn 的 GridSearchRandomizedSearch,结合 GridSearchCVRandomizedSearchCV 的内部交叉验证,将调参提升到新水平。您将用它们在多个参数的取值组合上同时搜索,从而穷举找到表现最好的模型。让我们从 GridSearchCV 开始动手吧!

本练习是课程的一部分

使用 XGBoost 的极端梯度提升

查看课程

练习说明

  • 创建名为 gbm_param_grid 的参数网格,其中包含 "colsample_bytree" 的取值列表(0.30.7),"n_estimators" 的单值列表(50),以及 2 个 "max_depth" 的取值列表(25)。
  • 实例化一个名为 gbmXGBRegressor 对象。
  • 创建名为 grid_mseGridSearchCV 对象,传入:将参数网格赋给 param_grid,将 XGBRegressor 赋给 estimator,将 "neg_mean_squared_error" 赋给 scoring,将 4 赋给 cv。同时指定 verbose=1,以便更好地理解输出。
  • GridSearchCV 对象拟合到 Xy
  • 分别使用 grid_mse.best_params_.best_score_ 属性,打印最优参数值和最低 RMSE。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create the parameter grid: gbm_param_grid
gbm_param_grid = {
    '____': [____, ____],
    '____': [____],
    '____': [____, ____]
}

# Instantiate the regressor: gbm
gbm = ____

# Perform grid search: grid_mse
grid_mse = ____


# Fit grid_mse to the data
____

# Print the best parameters and lowest RMSE
print("Best parameters found: ", ____)
print("Lowest RMSE found: ", np.sqrt(np.abs(____)))
编辑并运行代码