开始使用免费开始使用

调优 colsample_bytree

现在,来调优 "colsample_bytree"。如果您用过 scikit-learn 的 RandomForestClassifierRandomForestRegressor,应该见过类似的参数 max_features。在 xgboostsklearn 中,这个参数(虽然命名不同)都用于指定在每棵树的每次分裂时可供选择的特征比例。在 xgboost 中,colsample_bytree 必须设为 0 到 1 之间的浮点数。

本练习是课程的一部分

使用 XGBoost 的极端梯度提升

查看课程

练习说明

  • 创建名为 colsample_bytree_vals 的列表,存放取值 0.10.50.81
  • 像之前对 max_deptheta 所做的一样,系统地改变 "colsample_bytree" 并进行交叉验证。

交互式实操练习

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

# Create your housing DMatrix
housing_dmatrix = xgb.DMatrix(data=X,label=y)

# Create the parameter dictionary
params={"objective":"reg:squarederror","max_depth":3}

# Create list of hyperparameter values: colsample_bytree_vals
____ = ____
best_rmse = []

# Systematically vary the hyperparameter value 
for curr_val in ____:

    ____ = ____
    
    # Perform cross-validation
    cv_results = xgb.cv(dtrain=housing_dmatrix, params=params, nfold=2,
                 num_boost_round=10, early_stopping_rounds=5,
                 metrics="rmse", as_pandas=True, seed=123)
    
    # Append the final round rmse to best_rmse
    best_rmse.append(cv_results["test-rmse-mean"].tail().values[-1])

# Print the resultant DataFrame
print(pd.DataFrame(list(zip(colsample_bytree_vals, best_rmse)), columns=["colsample_bytree","best_rmse"]))
编辑并运行代码