gpt4 book ai didi

r - 使用插入符号指定交叉验证折叠

转载 作者:行者123 更新时间:2023-12-02 02:58:57 26 4
gpt4 key购买 nike

您好,提前致谢。我正在使用 caret 交叉验证 nnet 包中的神经网络。在 trainControl 函数的 method 参数中,我可以指定交叉验证类型,但所有这些都随机选择观察结果进行交叉验证。无论如何,我是否可以使用插入符号通过 ID 或硬编码参数来交叉验证数据中的特定观察结果?例如,这是我当前的代码:

library(nnet) 
library(caret)
library(datasets)

data(iris)

train.control <- trainControl(
method = "repeatedcv"
, number = 4
, repeats = 10
, verboseIter = T
, returnData = T
, savePredictions = T
)

tune.grid <- expand.grid(
size = c(2,4,6,8)
,decay = 2^(-3:1)
)

nnet.train <- train(
x = iris[,1:4]
, y = iris[,5]
, method = "nnet"
, preProcess = c("center","scale")
, metric = "Accuracy"
, trControl = train.control
, tuneGrid = tune.grid
)
nnet.train
plot(nnet.train)

假设我想将另一列 CV_GROUP 添加到 iris 数据框中,并且我希望插入符能够根据值为 的观察结果交叉验证神经网络>1 该列:

iris$CV_GROUP <- c(rep.int(0,times=nrow(iris)-20), rep.int(1,times=20))

这可以用插入符实现吗?

最佳答案

使用indexindexOut控制选项。我编写了一种方法来实现此目的,让您可以选择所需的重复次数和折叠次数:

library(nnet)
library(caret)
library(datasets)
library(data.table)
library(e1071)

r <- 2 # number of repeats
k <- 5 # number of folds
data(iris)
iris <- data.table(iris)

# Create folds and repeats here - you could create your own if you want #
set.seed(343)
for (i in 1:r) {
newcol <- paste('fold.num',i,sep='')
iris <- iris[,eval(newcol):=sample(1:k, size=dim(iris)[1], replace=TRUE)]
}

folds.list.out <- list()
folds.list <- list()
list.counter <- 1
for (y in 1:r) {
newcol <- paste('fold.num', y, sep='')
for (z in 1:k) {
folds.list.out[[list.counter]] <- which(iris[,newcol,with=FALSE]==z)
folds.list[[list.counter]] <- which(iris[,newcol,with=FALSE]!=z)
list.counter <- list.counter + 1
}
iris <- iris[,!newcol,with=FALSE]
}

tune.grid <- expand.grid(
size = c(2,4,6,8)
,decay = 2^(-3:1)
)

train.control <- trainControl(
index=folds.list
, indexOut=folds.list.out
, verboseIter = T
, returnData = T
, savePredictions = T
)

iris <- data.frame(iris)

nnet.train <- train(
x = iris[,1:4]
, y = iris[,5]
, method = "nnet"
, preProcess = c("center","scale")
, metric = "Accuracy"
, trControl = train.control
, tuneGrid = tune.grid
)

nnet.train
plot(nnet.train)

关于r - 使用插入符号指定交叉验证折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32077931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com