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

การปรับค่า colsample_bytree

ถึงเวลาปรับค่า "colsample_bytree" กันแล้ว พารามิเตอร์นี้คงคุ้นเคยดีถ้าเคยใช้ RandomForestClassifier หรือ RandomForestRegressor ของ scikit-learn มาก่อน เพราะในนั้นจะรู้จักกันในชื่อ max_features ทั้งใน xgboost และ sklearn พารามิเตอร์นี้ (แม้จะต่างชื่อกัน) ทำหน้าที่เดียวกัน คือกำหนดสัดส่วนของ feature ที่จะสุ่มเลือกใช้ในแต่ละการแยกสาขาของต้นไม้ โดยใน xgboost ค่า colsample_bytree ต้องเป็นทศนิยมระหว่าง 0 ถึง 1

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Extreme Gradient Boosting with XGBoost

ดูคอร์ส

คำแนะนำการฝึกหัด

  • สร้างลิสต์ชื่อ colsample_bytree_vals เพื่อเก็บค่า 0.1, 0.5, 0.8 และ 1
  • ปรับค่า "colsample_bytree" อย่างเป็นระบบแล้วทำ cross-validation โดยใช้วิธีเดียวกับที่เคยทำกับ max_depth และ eta ก่อนหน้านี้

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Create your housing DMatrix
housing_dmatrix = xgb.DMatrix(data=X,label=y)

# Create the parameter dictionary
params={"objective":"reg:squarederror","max_depth":3}

# Create list of hyperparameter values: colsample_bytree_vals
____ = ____
best_rmse = []

# Systematically vary the hyperparameter value 
for curr_val in ____:

    ____ = ____
    
    # Perform cross-validation
    cv_results = xgb.cv(dtrain=housing_dmatrix, params=params, nfold=2,
                 num_boost_round=10, early_stopping_rounds=5,
                 metrics="rmse", as_pandas=True, seed=123)
    
    # 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(colsample_bytree_vals, best_rmse)), columns=["colsample_bytree","best_rmse"]))
แก้ไขและรันโค้ด