调优 eta
现在是系统练习调优其他 XGBoost 超参数并观察其对模型性能影响的时候了!您将从调优 "eta"(也称为学习率)开始。
在 XGBoost 中,学习率是一个介于 0 和 1 之间的参数。"eta" 值越高,对特征权重的惩罚越强,正则化也就越强。
本练习是课程的一部分
使用 XGBoost 的极端梯度提升
练习说明
- 创建一个名为
eta_vals的列表,存储以下"eta"值:0.001、0.01和0.1。 - 使用
for循环遍历eta_vals列表。 - 在
for循环的每次迭代中,将params的"eta"键设置为curr_val。然后执行 3 折交叉验证,包含提前停止(5轮)、10轮 boosting,评估指标为"rmse",并将seed设为123。请确保输出为 DataFrame。 - 将最终一轮的 RMSE 追加到
best_rmse列表中。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create your housing DMatrix: housing_dmatrix
housing_dmatrix = xgb.DMatrix(data=X, label=y)
# Create the parameter dictionary for each tree (boosting round)
params = {"objective":"reg:squarederror", "max_depth":3}
# Create list of eta values and empty list to store final round rmse per xgboost model
____ = [____, ____, ____]
best_rmse = []
# Systematically vary the eta
for curr_val in ____:
params["___"] = curr_val
# Perform cross-validation: cv_results
cv_results = ____
# Append the final round rmse to best_rmse
____.____(____["____"].tail().values[-1])
# Print the resultant DataFrame
print(pd.DataFrame(list(zip(eta_vals, best_rmse)), columns=["eta","best_rmse"]))