始める無料で始める

colsample_bytree のチューニング

それでは、"colsample_bytree" をチューニングしていきます。これは、scikit-learn の RandomForestClassifierRandomForestRegressor を使ったことがあれば、max_features として見たことがあるはずです。xgboostsklearn のどちらでも、このパラメータ(名称は異なりますが)は、各決定木の分岐ごとに候補とする特徴量の割合を指定します。xgboost では、colsample_bytree は 0 以上 1 以下の浮動小数で指定します。

この演習はコースの一部です

XGBoost で学ぶ極限の勾配ブースティング

コースを見る

演習の手順

  • 0.10.50.81 を格納するリスト colsample_bytree_vals を作成します。
  • 以前に max_deptheta で行ったのと全く同じ要領で、"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"]))
コードを編集して実行