在 R 中使用插入符号重复平衡 k 折交叉验证
Repeated balanced k-fold cross validation using caret in R
我想使用 caret
包执行重复的 k 折交叉验证。这可以在 trainControl()
函数中指定。
我的问题是,使用 trainControl(method="repeatedcv", number=k, repeats=n)
创建的折叠是否平衡?这些 k 折的生成方式是否与 createFolds()
生成的平衡折的方式相同?
为清楚起见,以下是平衡和不平衡 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 折的 class 细分。
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
答案是肯定的。
如果 method = "repeatedcv"
它调用函数 createMultiFolds
,它在内部调用 createFolds
,但在 repeats = n
中指定了 n 次
我想使用 caret
包执行重复的 k 折交叉验证。这可以在 trainControl()
函数中指定。
我的问题是,使用 trainControl(method="repeatedcv", number=k, repeats=n)
创建的折叠是否平衡?这些 k 折的生成方式是否与 createFolds()
生成的平衡折的方式相同?
为清楚起见,以下是平衡和不平衡 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 折的 class 细分。
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
答案是肯定的。
如果 method = "repeatedcv"
它调用函数 createMultiFolds
,它在内部调用 createFolds
,但在 repeats = n