【问题标题】:How to use custom cross validation folds with XGBoost如何在 XGBoost 中使用自定义交叉验证折叠
【发布时间】:2018-03-07 21:59:59
【问题描述】:

我正在为 XGBoost 使用 R 包装器。在函数xgb.cv中,有一个folds参数说明

list 提供了使用预定义 CV 折叠列表的可能性 (每个元素必须是折叠索引的向量)。如果折叠是 提供时,将忽略 nfold 和分层参数。

那么,我是否只指定用于训练模型的索引并假设其余的将用于测试?例如,如果我的训练数据类似于

    Feature1 Feature2 Target
 1:        2       10     10
 2:        7        1      9
 3:        8        2      3
 4:        8       10      7
 5:        8        2      9
 6:        3        7      3

并且我想使用 ((1,2,3), (4,5,6)) 和 ((4,5,6), (1,2,3) 的 (train, test) 索引进行交叉验证)) 我要设置folds=list(c(1,2,3), c(4,5,6))吗?

【问题讨论】:

  • caret::createFoldscaret::createDataPartition 中的一个会为您完成艰苦的工作。你的例子可能是正确的。

标签: r xgboost


【解决方案1】:

通过反复试验,我发现xgboost 正在使用传递的索引作为 test 折叠的索引。通过注意到xgboost 的当前开发版本在documentation 中明确声明,确认了这一点。

【讨论】:

  • 自定义折叠的结果与常规 CV 大不相同。描述我在做什么:xgb.DMatrix 有 2000 行。 folds 应该有测试索引。所以,folds=list(1000:2000, 1500:2000, 1750:2000)。它正在做袋内预测......
【解决方案2】:

这是一个生成折叠和使用它们的示例。

假设在我们的数据框中,我们有一列 id,因此我们希望将具有给定 id 值的所有行放在一个折叠中。

下面的代码

  • 查找唯一 ID
  • 为折叠预分配一个列表
  • 遍历 id,创建匹配的行索引列表

    fold.ids <- unique(df$id) custom.folds <- vector("list", length(fold.ids)) i <- 1 for( id in fold.ids){ custom.folds[[i]] <- which( df$id %in% id ) i <- i+1 }

这是一个在xgb.cv中使用上述折叠列表的示例

res &lt;- xgb.cv(param, dtrain, nround, folds=custom.folds, prediction = TRUE)

其他xgb.cv参数的合理值可以是found in the documentation

【讨论】:

    【解决方案3】:

    这对我来说效果最好:

    custom.folds <- caret::createFolds(data$Label, k=10, list=T)
    
    xgbcv <- xgb.cv(
      params = params
      ,data = df
      ,maximize = F
      ,prediction = T
      ,metrics = "logloss"
      ,folds = custom.folds
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-17
      • 2016-08-17
      • 2014-05-12
      • 2018-08-02
      • 2015-11-11
      • 1970-01-01
      • 2014-11-08
      • 2021-03-18
      相关资源
      最近更新 更多