เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การปรับแต่ง 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"]))
แก้ไขและรันโค้ด