ブースティングラウンド数のチューニング
まずは、ブースティングラウンド数(構築する木の本数)が、XGBoost モデルの汎化性能にどう影響するかを見ていきましょう。for ループ内で xgb.cv() を使い、num_boost_round ごとに1つずつモデルを学習します。
ここでは Ames 住宅価格データセットを引き続き使用します。特徴量は配列 X に、目的変数はベクトル y に格納されています。
この演習はコースの一部です
XGBoost で学ぶ極限の勾配ブースティング
演習の手順
XとyからDMatrixを作成し、housing_dmatrixという名前を付けます。paramsという名前のパラメータ辞書を作成し、適切な"objective"("reg:squarederror")と"max_depth"(3に設定)を指定します。forループでnum_roundsを反復し、3 分割の交差検証を実行します。各反復で、現在のブースティングラウンド数(curr_num_rounds)をxgb.cv()のnum_boost_round引数に渡します。- 各クロスバリデーション済み XGBoost モデルの最終ラウンドの RMSE を、
final_rmse_per_roundリストに追加します。 num_roundsとfinal_rmse_per_roundは zip して DataFrame に変換済みです。ラウンド数ごとのモデル性能を簡単に確認できます。"Submit Answer" を押して結果を見てみましょう!
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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"]))