【发布时间】: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