【问题标题】:Repeated balanced k-fold cross validation using caret in R在 R 中使用插入符号重复平衡 k 折交叉验证
【发布时间】:2017-02-05 15:22:24
【问题描述】:

我想使用 caret 包执行重复的 k 折交叉验证。这可以在trainControl() 函数中指定。

我的问题是,使用trainControl(method="repeatedcv", number=k, repeats=n) 创建的折叠是否平衡?这些k-folds的生成方式是否与createFolds()生成的平衡k-folds一样?


为清楚起见,以下是平衡和不平衡 k 折叠的示例:

iris 物种细分:

table(iris$Species)
# setosa versicolor  virginica 
#     50         50         50

现在,我们创建随机的不平衡和平衡折叠:

k <- 10

unbalanced <- sample(rep(seq(k), length=length(iris$Species)))

bList <- createFolds(iris$Species, k)

# Below, we reformat the list of folds
balanced <- rep(-1, length(iris$Species))
for (i in seq_len(k)) balanced[bList[[i]]] <- i

现在,我们可视化每组 k 折叠的类别分解。

classBreakdownTable <- function(i, folds) table(as.factor(iris$Species)[which(folds == i)])

sapply(seq_len(k), classBreakdownTable, unbalanced)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# setosa        4    6    8    4    4    4    7    6    5     2
# versicolor    5    5    1    5    5    7    4    6    6     6
# virginica     6    4    6    6    6    4    4    3    4     7

sapply(seq_len(k), classBreakdownTable, balanced)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# setosa        5    5    5    5    5    5    5    5    5     5
# versicolor    5    5    5    5    5    5    5    5    5     5
# virginica     5    5    5    5    5    5    5    5    5     5

【问题讨论】:

    标签: r r-caret cross-validation


    【解决方案1】:

    答案是肯定的。

    如果method = "repeatedcv" 调用函数createMultiFolds,该函数在内部调用createFolds,但在repeats = n 中指定n 次

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 2016-09-30
      • 2016-11-15
      • 2015-12-13
      • 2015-05-19
      • 2016-01-15
      • 2015-11-11
      • 2019-12-18
      • 2021-04-06
      相关资源
      最近更新 更多