การปรับจำนวน Boosting Rounds
เริ่มต้นการปรับพารามิเตอร์ด้วยการดูว่าจำนวน boosting rounds (จำนวนต้นไม้ที่สร้าง) ส่งผลต่อประสิทธิภาพของโมเดล XGBoost บนข้อมูลที่ไม่ได้ใช้ฝึกอย่างไร โดยจะใช้ xgb.cv() ภายใน for loop และสร้างโมเดลหนึ่งโมเดลต่อค่าพารามิเตอร์ num_boost_round
ในแบบฝึกหัดนี้ จะทำงานต่อเนื่องกับชุดข้อมูลราคาบ้าน Ames โดย feature ต่าง ๆ อยู่ในอาร์เรย์ X และ target vector อยู่ใน y
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Extreme Gradient Boosting with XGBoost
คำแนะนำการฝึกหัด
- สร้าง
DMatrixชื่อhousing_dmatrixจากXและy - สร้าง parameter dictionary ชื่อ
paramsโดยระบุ"objective"("reg:squarederror") และ"max_depth"(กำหนดเป็น3) ให้ถูกต้อง - วนซ้ำผ่าน
num_roundsในforloop และทำ cross-validation แบบ 3-fold ในแต่ละรอบของ loop ให้ส่งจำนวน boosting rounds ปัจจุบัน (curr_num_rounds) ไปยังxgb.cv()เป็นอาร์กิวเมนต์ของnum_boost_round - เพิ่มค่า RMSE ของ boosting round สุดท้ายสำหรับโมเดล XGBoost แต่ละตัวที่ผ่านการ cross-validate ลงใน list
final_rmse_per_round num_roundsและfinal_rmse_per_roundถูก zip และแปลงเป็น DataFrame แล้ว เพื่อให้ดูประสิทธิภาพของโมเดลในแต่ละ boosting round ได้ง่ายขึ้น กด 'ส่งคำตอบ' เพื่อดูผลลัพธ์!
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Create the DMatrix: housing_dmatrix
housing_dmatrix = ____
# Create the parameter dictionary for each tree: params
params = {"____":"____", "____":____}
# Create list of number of boosting rounds
num_rounds = [5, 10, 15]
# Empty list to store final round rmse per XGBoost model
final_rmse_per_round = []
# Iterate over num_rounds and build one model per num_boost_round parameter
for curr_num_rounds in num_rounds:
# Perform cross-validation: cv_results
cv_results = ____(dtrain=____, params=____, nfold=3, num_boost_round=____, metrics="rmse", as_pandas=True, seed=123)
# Append final round RMSE
____.____(cv_results["test-rmse-mean"].tail().values[-1])
# Print the resultant DataFrame
num_rounds_rmses = list(zip(num_rounds, final_rmse_per_round))
print(pd.DataFrame(num_rounds_rmses,columns=["num_boosting_rounds","rmse"]))