调优 colsample_bytree
现在,来调优 "colsample_bytree"。如果您用过 scikit-learn 的 RandomForestClassifier 或 RandomForestRegressor,应该见过类似的参数 max_features。在 xgboost 和 sklearn 中,这个参数(虽然命名不同)都用于指定在每棵树的每次分裂时可供选择的特征比例。在 xgboost 中,colsample_bytree 必须设为 0 到 1 之间的浮点数。
本练习是课程的一部分
使用 XGBoost 的极端梯度提升
练习说明
- 创建名为
colsample_bytree_vals的列表,存放取值0.1、0.5、0.8和1。 - 像之前对
max_depth和eta所做的一样,系统地改变"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"]))