Tinh chỉnh colsample_bytree
Giờ là lúc tinh chỉnh "colsample_bytree". Bạn sẽ thấy quen nếu từng dùng RandomForestClassifier hoặc RandomForestRegressor của scikit-learn, nơi tham số này được gọi là max_features. Trong cả xgboost và sklearn, tham số này (dù tên khác nhau) đơn giản là chỉ định tỷ lệ đặc trưng được chọn tại mỗi lần chia trong một cây. Trong xgboost, colsample_bytree phải là một số thực trong khoảng từ 0 đến 1.
Bài tập này là một phần của khóa học
Gradient Boosting Cực Mạnh với XGBoost
Hướng dẫn bài tập
- Tạo danh sách tên
colsample_bytree_valsđể lưu các giá trị0.1,0.5,0.8, và1. - Thay đổi có hệ thống
"colsample_bytree"và thực hiện cross-validation, giống hệt như bạn đã làm vớimax_depthvàetatrước đó.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# 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"]))