开始使用免费开始使用

以决策树作为基学习器

现在是构建 XGBoost 模型来预测房价的时候了——不是视频中看到的马萨诸塞州波士顿,而是爱荷华州 Ames!这个房价数据集已预加载为名为 df 的 DataFrame。若您在 Shell 中查看,会发现包含多种关于房屋及其在城市中位置的特征。

在本练习中,您的目标是使用树作为基学习器。默认情况下,XGBoost 使用树作为基学习器,因此无需通过 booster="gbtree" 指定使用树。

xgboost 已以 xgb 名称导入,特征和目标的数组分别可在 Xy 中使用。

本练习是课程的一部分

使用 XGBoost 的极端梯度提升

查看课程

练习说明

  • df 划分为训练集和测试集,留出 20% 作为测试集。使用 random_state123
  • XGBRegressor 实例化为 xg_reg,使用 seed123。指定 objective 为 "reg:squarederror",并使用 10 棵树。注意:无需指定 booster="gbtree",因为这是默认值。
  • 拟合 xg_reg 到训练数据,并预测测试集的标签。将预测结果保存到名为 preds 的变量中。
  • 使用 np.sqrt() 和已预导入的 sklearn.metrics 中的 mean_squared_error() 函数计算 rmse

交互式实操练习

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

# Create the training and test sets
X_train, X_test, y_train, y_test = ____(____, ____, ____=____, random_state=123)

# Instantiate the XGBRegressor: xg_reg
xg_reg = ____

# Fit the regressor to the training set
____

# Predict the labels of the test set: preds
preds = ____

# Compute the rmse: rmse
rmse = ____(____(____, ____))
print("RMSE: %f" % (rmse))
编辑并运行代码