使用插入符号指定交叉验证折叠
Specify Cross Validation Folds with caret
您好,提前致谢。我正在使用 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))
caret
可以吗?
使用index
和indexOut
控制选项。我编写了一种实现此方法的方法,让您 select 您想要的重复和折叠次数:
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)
您好,提前致谢。我正在使用 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))
caret
可以吗?
使用index
和indexOut
控制选项。我编写了一种实现此方法的方法,让您 select 您想要的重复和折叠次数:
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)