調整 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個提升回合,評估指標為"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"]))