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

การปรับจำนวน 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 ใน for loop และทำ 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"]))
แก้ไขและรันโค้ด