开始使用免费开始使用

训练 XGBoost 模型

每种机器学习方法都有可能过拟合。您将通过 XGBoost 的示例看到这一点。我们仍然使用 Store Item Demand Forecasting Challenge 的数据。train DataFrame 已在您的工作空间中可用。

首先,使用 XGBoost 的学习 API 训练多组超参数的多个模型。本练习中,您只需调整一个超参数:

  • max_depth —— 树的最大深度。增大该值会使模型更复杂,也更容易过拟合。

本练习是课程的一部分

用 Python 赢下 Kaggle 竞赛

查看课程

交互式实操练习

通过完成这段示例代码来试试这个练习。

import xgboost as xgb

# Create DMatrix on train data
dtrain = xgb.DMatrix(data=train[['store', 'item']],
                     label=train['sales'])

# Define xgboost parameters
params = {'objective': 'reg:squarederror',
          '____': ____,
          'verbosity': 0}

# Train xgboost model
xg_depth_2 = xgb.train(params=params, dtrain=dtrain)
编辑并运行代码