網格搜尋
回想一下,我們在上一個課程單元已經建立了基準的 Gradient Boosting 模型。你現在的目標是為這個 Gradient Boosting 模型找出最佳的 max_depth 超參數值。這個超參數會限制每棵樹中的節點數量。你將使用 K 折交叉驗證來衡量每個超參數值下模型的本地效能。
系統已提供函式 get_cv_score(),它會以訓練資料集與模型參數字典作為引數,並回傳在 3 折交叉驗證下的整體驗證 RMSE 分數。
本練習屬於課程
用 Python 拿下 Kaggle 競賽
練習說明
- 指定可能的
max_depth值組成的網格:3、6、9、12、15。 - 將網格中的每個超參數候選值傳入模型的
params字典。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Possible max depth values
max_depth_grid = [____]
results = {}
# For each value in the grid
for max_depth_candidate in max_depth_grid:
# Specify parameters for the model
params = {'max_depth': ____}
# Calculate validation score for a particular hyperparameter
validation_score = get_cv_score(train, params)
# Save the results for each max depth value
results[max_depth_candidate] = validation_score
print(results)