開始使用免費開始

調整 eta

現在來實作調整其他 XGBoost 超參數,並觀察它們對模型效能的影響!你將從調整 "eta"(也稱為學習率)開始。

在 XGBoost 中,學習率是一個介於 01 的參數;"eta" 值越高,對特徵權重的懲罰越強,正規化也會更強。

本練習屬於課程

使用 XGBoost 的極端梯度提升

檢視課程

練習說明

  • 建立名為 eta_vals 的清單,儲存以下 "eta" 值:0.0010.010.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"]))
編輯並執行程式碼