開始使用免費開始

微調 max_depth

在這個練習中,你要微調 max_depth。這個參數決定在每一輪 boosting 中,每棵樹所能生長的最大深度。較小的值會得到較淺的樹,較大的值會得到較深的樹。

本練習屬於課程

使用 XGBoost 的極端梯度提升

檢視課程

練習說明

  • 建立一個名為 max_depths 的列表,存放以下 "max_depth" 值:251020
  • 使用 for 迴圈走訪你的 max_depths 列表。
  • for 迴圈的每次迭代中系統性地變動 "max_depth",並執行具早停(5 輪)的 2 折交叉驗證、10 輪 boosting、評估指標為 "rmse",以及 seed 設為 123。請確保輸出為 DataFrame。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

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

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

# Create list of max_depth values
max_depths = ____
best_rmse = []

# Systematically vary the max_depth
for curr_val in ____:

    params["____"] = ____
    
    # Perform cross-validation
    cv_results = ____
    
    
    
    # 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(max_depths, best_rmse)),columns=["max_depth","best_rmse"]))
編輯並執行程式碼