开始使用免费开始使用

衡量准确率

现在,您将通过使用 XGBoost 内置的交叉验证功能来练习其学习 API。正如 Sergey 在上一段视频中所讲,XGBoost 之所以性能出色、效率更高,是因为它为数据集使用了名为 DMatrix 的优化数据结构。

在上一个练习中,输入数据集是在运行时被即时转换为 DMatrix 的;但当您使用 xgboostcv 对象时,需要先显式地将数据转换为 DMatrix。因此,在对 churn_data 运行交叉验证之前,您需要先完成这一转换。

本练习是课程的一部分

使用 XGBoost 的极端梯度提升

查看课程

练习说明

  • 使用 xgb.DMatrix()churn_data 创建名为 churn_dmatrixDMatrix。特征在 X 中,标签在 y 中。
  • 通过调用 xgb.cv() 执行 3 折交叉验证。dtrain 为您的 churn_dmatrixparams 为您的参数字典,nfold 为交叉验证折数(3),num_boost_round 为要构建的树的数量(5),metrics 为您要计算的指标(这里为 "error",我们稍后会将其转换为准确率)。

交互式实操练习

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

# Create arrays for the features and the target: X, y
X, y = churn_data.iloc[:,:-1], churn_data.iloc[:,-1]

# Create the DMatrix from X and y: churn_dmatrix
churn_dmatrix = ____(data=____, label=____)

# Create the parameter dictionary: params
params = {"objective":"reg:logistic", "max_depth":3}

# Perform cross-validation: cv_results
cv_results = ____(dtrain=____, params=____, 
                  nfold=____, num_boost_round=____, 
                  metrics="____", as_pandas=____, seed=123)

# Print cv_results
print(cv_results)

# Print the accuracy
print(((1-cv_results["test-error-mean"]).iloc[-1]))
编辑并运行代码