衡量准确率
现在,您将通过使用 XGBoost 内置的交叉验证功能来练习其学习 API。正如 Sergey 在上一段视频中所讲,XGBoost 之所以性能出色、效率更高,是因为它为数据集使用了名为 DMatrix 的优化数据结构。
在上一个练习中,输入数据集是在运行时被即时转换为 DMatrix 的;但当您使用 xgboost 的 cv 对象时,需要先显式地将数据转换为 DMatrix。因此,在对 churn_data 运行交叉验证之前,您需要先完成这一转换。
本练习是课程的一部分
使用 XGBoost 的极端梯度提升
练习说明
- 使用
xgb.DMatrix()从churn_data创建名为churn_dmatrix的DMatrix。特征在X中,标签在y中。 - 通过调用
xgb.cv()执行 3 折交叉验证。dtrain为您的churn_dmatrix,params为您的参数字典,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]))