如何理解R包xgboost中的nfold和nrounds

How to understand nfold and nrounds in R's package xgboost

我正在尝试使用 R 的包 xgboost。但有一点让我感到困惑。在 xgboost 手册中,在 xgb.cv 函数下,它说:

原始样本被随机分成 n 倍大小相等的子样本。

nfold子样本中,保留一个子样本作为验证数据用于测试模型,其余nfold-1个子样本作为训练数据。

然后交叉验证过程重复 n 轮次,每个 n 倍子样本 仅使用一次作为验证数据。

这是手册中的代码:

data(agaricus.train, package='xgboost')
dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
cv <- xgb.cv(data = dtrain, nrounds = 3, nthread = 2, nfold = 5, metrics = 
list("rmse","auc"),
max_depth = 3, eta = 1, objective = "binary:logistic")
print(cv)
print(cv, verbose=TRUE)

结果是:

##### xgb.cv 5-folds
call:
  xgb.cv(data = dtrain, nrounds = 3, nfold = 5, metrics = list("rmse", 
    "auc"), nthread = 2, max_depth = 3, eta = 1, objective = "binary:logistic")
params (as set within xgb.cv):
  nthread = "2", max_depth = "3", eta = "1", objective = "binary:logistic", 
eval_metric = "rmse", eval_metric = "auc", silent = "1"
callbacks:
  cb.print.evaluation(period = print_every_n, showsd = showsd)
  cb.evaluation.log()
niter: 3
evaluation_log:
 iter train_rmse_mean train_rmse_std train_auc_mean train_auc_std test_rmse_mean test_rmse_std test_auc_mean test_auc_std
1       0.1623756    0.002693092      0.9871108  1.123550e-03      0.1625222   0.009134128     0.9870954 0.0045008818
2       0.0784902    0.002413883      0.9998370  1.317346e-04      0.0791366   0.004566554     0.9997756 0.0003538184
3       0.0464588    0.005172930      0.9998942  7.315846e-05      0.0478028   0.007763252     0.9998902 0.0001347032

假设 nfold=5 和 nrounds=2。这意味着数据被分成 5 个大小相等的部分。该算法将生成 2 棵树。

我的理解是:每个子样本都要验证一次。当一个子样本被验证时,将生成 2 棵树。因此,我们将有 5 组树(一组有 2 棵树,因为 nrounds=2)。然后我们检查评估指标是否变化很大。

但结果却不尽相同。一个 nround 值有一行评估指标,看起来它已经包含了 'cross validation' 部分。所以,如果 'The cross-validation process is then repeated nrounds times',那么 'with each of the nfold subsamples used exactly once as the validation data' 怎么来的?

这些是nfold拟合分数的均值标准差-在 n 轮 的每一轮中测试程序 运行。 XGBoost 交叉验证过程是这样进行的:

  1. 数据集 X 被分成 n 倍 个子样本,X1,X2。 ..Xn倍.
  2. XGBoost 算法将增强树拟合到包含 X1、X2、...、X[= 的训练数据集29=]nfold-1,而最后一个子样本(fold)Xnfold被阻止作为验证1(out -样本)数据集。为训练和验证数据集计算并保留所选的评估指标(RMSE、AUC 等)。
  3. 训练数据集中的一个子样本(折叠)现在与验证子样本(折叠)交换,因此训练数据集现在包含 X1、X 2, ... , Xnfold-2, Xnfold 和验证(样本外)数据集是Xn倍-1。该算法再次将提升树拟合到训练数据,计算评估分数(针对每个选定的指标)等等。
  4. 这个过程重复 n 倍 次,直到每个子样本(折叠)都作为训练集的一部分 作为验证放。
  5. 现在,添加了另一棵提升树,并重复步骤 2-4 中概述的过程。这一直持续到适合训练数据的提升树总数等于 nrounds.
  6. 现在 nfold 计算的评估分数(乘以所选不同指标的数量)在 nrounds 中的每一轮都用于训练集和验证集(验证集上的分数自然会更差)。 nfold 分数的均值和标准差是针对 nrounds[=50] 中每一轮的训练集和验证集计算的(乘以所选不同指标的数量) =] 并在具有 nrounds 行的数据框中返回。

1 请注意,我所说的 'validation' 集在评估日志

中被 XGBoost 识别为 'test' 集