การปรับแต่ง max_depth
ในแบบฝึกหัดนี้ งานของคุณคือการปรับแต่ง max_depth ซึ่งเป็นพารามิเตอร์ที่กำหนดความลึกสูงสุดที่แต่ละต้นไม้ในรอบ boosting สามารถเติบโตได้ ค่าที่น้อยกว่าจะทำให้ต้นไม้มีความลึกน้อยลง ส่วนค่าที่มากกว่าจะทำให้ต้นไม้มีความลึกมากขึ้น
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Extreme Gradient Boosting with XGBoost
คำแนะนำการฝึกหัด
- สร้างลิสต์ชื่อ
max_depthsเพื่อเก็บค่า"max_depth"ต่อไปนี้:2,5,10และ20 - วนซ้ำผ่านลิสต์
max_depthsโดยใช้ลูปfor - ปรับเปลี่ยนค่า
"max_depth"อย่างเป็นระบบในแต่ละรอบของลูปforและทำ cross-validation แบบ 2-fold พร้อม early stopping (5รอบ), boosting จำนวน10รอบ, เมตริกเป็น"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"]))