colsample_bytree のチューニング
それでは、"colsample_bytree" をチューニングしていきます。これは、scikit-learn の RandomForestClassifier や RandomForestRegressor を使ったことがあれば、max_features として見たことがあるはずです。xgboost と sklearn のどちらでも、このパラメータ(名称は異なりますが)は、各決定木の分岐ごとに候補とする特徴量の割合を指定します。xgboost では、colsample_bytree は 0 以上 1 以下の浮動小数で指定します。
この演習はコースの一部です
XGBoost で学ぶ極限の勾配ブースティング
演習の手順
0.1、0.5、0.8、1を格納するリストcolsample_bytree_valsを作成します。- 以前に
max_depthとetaで行ったのと全く同じ要領で、"colsample_bytree"を系統的に変化させてクロスバリデーションを実行します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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"]))