始める無料で始める

XGBoost モデルのクロスバリデーション

この演習では、作成したパイプラインを使って前処理を行い、さらにモデルのクロスバリデーションも実施します。

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

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

コースを見る

演習の手順

  • steps を使って xgb_pipeline という名前のパイプラインを作成します。
  • cross_val_score() を使って 10 分割のクロスバリデーションを実行します。パイプライン、X.to_dict("records") で辞書に変換したもの)、y、分割数、scoring"neg_mean_squared_error")を渡します。
  • 10 分割の RMSE を出力します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Import necessary modules
from sklearn.feature_extraction import DictVectorizer
from sklearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score

# Fill LotFrontage missing values with 0
X.LotFrontage = ____

# Setup the pipeline steps: steps
steps = [("ohe_onestep", DictVectorizer(sparse=False)),
         ("xgb_model", xgb.XGBRegressor(max_depth=2, objective="reg:squarederror"))]

# Create the pipeline: xgb_pipeline
xgb_pipeline = ____

# Cross-validate the model
cross_val_scores = ____

# Print the 10-fold RMSE
print("10-fold RMSE: ", np.mean(np.sqrt(np.abs(____))))
コードを編集して実行