2011-09-13 122 views
13

如何自动分割使用R进行5次交叉验证的矩阵? 我其实想生成5套(test_matrix_indices,train matrix_indices)。为交叉验证生成集

+0

请不要混淆你的问题。这变得令人困惑。如果你想回答你自己的问题,那么请在新的答案中这样做。 – Andrie

+0

对于K倍交叉验证,您必须合并K-1个子集作为训练集,并留下一个作为测试(重复K次),所以这不是针对您的问题的完整解决方案。 –

+0

我已将答案放入答案部分。 – Delphine

回答

15
f_K_fold <- function(Nobs,K=5){ 
    rs <- runif(Nobs) 
    id <- seq(Nobs)[order(rs)] 
    k <- as.integer(Nobs*seq(1,K-1)/K) 
    k <- matrix(c(0,rep(k,each=2),Nobs),ncol=2,byrow=TRUE) 
    k[,1] <- k[,1]+1 
    l <- lapply(seq.int(K),function(x,k,d) 
       list(train=d[!(seq(d) %in% seq(k[x,1],k[x,2]))], 
        test=d[seq(k[x,1],k[x,2])]),k=k,d=id) 
    return(l) 
} 
+0

这是一个优雅的解决方案。谢谢。 – Delphine

+0

此外,这种解决方案可以通过添加set.seed(n) – Delphine

+2

什么id d?我没有明白。 – LoveMeow

23

我想你想矩阵行是分裂的情况。然后,所有你需要的是samplesplit

X <- matrix(rnorm(1000),ncol=5) 
id <- sample(1:5,nrow(X),replace=TRUE) 
ListX <- split(x,id) # gives you a list with the 5 matrices 
X[id==2,] # gives you the second matrix 

我会用列表的工作,因为它可以让你做这样的事情:

names(ListX) <- c("Train1","Train2","Train3","Test1","Test2") 
mean(ListX$Train3) 

这使得代码更易于阅读,并使您不会在工作区中创建大量矩阵。如果您将矩阵分别放置在工作区中,您一定会搞砸。使用列表!

如果你想测试矩阵是比其他的更小或更大,使用prob说法sample

id <- sample(1:5,nrow(X),replace=TRUE,prob=c(0.15,0.15,0.15,0.15,0.3)) 

给你一个测试矩阵那是火车矩阵大小的两倍。

如果您想确定确切的病例数,则sampleprob不是最佳选择。你可以使用这样的技巧:

indices <- rep(1:5,c(100,20,20,20,40)) 
id <- sample(indices) 

得到分别为100,20,...和40的矩阵。

+0

+1分裂 - 我真的想知道为自己生成交叉验证矩阵,这是完美的。 – richiemorrisroe

+0

joris伟大的代码谢谢。 是不是有交叉验证的想法,你循环遍历所有集合,并使用每个组作为测试数据至少一次,这将打败使用列表的目的,并像你这样命名它? – appleLover

+0

@appleLover列表的使用仅仅是为了避免在工作区中生成单个矩阵。这是为了保持一切。交叉验证和自举有多种方法,根据方法,您需要对统计信息进行不同的更正。我只是给出了一种方法来有组织地创建这些矩阵。 –

4

解决方案,而分裂:

set.seed(7402313) 
X <- matrix(rnorm(999), ncol=3) 
k <- 5 # number of folds 

# Generating random indices 
id <- sample(rep(seq_len(k), length.out=nrow(X))) 
table(id) 
# 1 2 3 4 5 
# 67 67 67 66 66 

# lapply over them: 
indicies <- lapply(seq_len(k), function(a) list(
    test_matrix_indices = which(id==a), 
    train_matrix_indices = which(id!=a) 
)) 
str(indicies) 
# List of 5 
# $ :List of 2 
# ..$ test_matrix_indices : int [1:67] 12 13 14 17 18 20 23 28 41 45 ... 
# ..$ train_matrix_indices: int [1:266] 1 2 3 4 5 6 7 8 9 10 ... 
# $ :List of 2 
# ..$ test_matrix_indices : int [1:67] 4 19 31 36 47 53 58 67 83 89 ... 
# ..$ train_matrix_indices: int [1:266] 1 2 3 5 6 7 8 9 10 11 ... 
# $ :List of 2 
# ..$ test_matrix_indices : int [1:67] 5 8 9 30 32 35 37 56 59 60 ... 
# ..$ train_matrix_indices: int [1:266] 1 2 3 4 6 7 10 11 12 13 ... 
# $ :List of 2 
# ..$ test_matrix_indices : int [1:66] 1 2 3 6 21 24 27 29 33 34 ... 
# ..$ train_matrix_indices: int [1:267] 4 5 7 8 9 10 11 12 13 14 ... 
# $ :List of 2 
# ..$ test_matrix_indices : int [1:66] 7 10 11 15 16 22 25 26 40 42 ... 
# ..$ train_matrix_indices: int [1:267] 1 2 3 4 5 6 8 9 12 13 ... 

但是你可以返回矩阵太:

matrices <- lapply(seq_len(k), function(a) list(
    test_matrix = X[id==a, ], 
    train_matrix = X[id!=a, ] 
)) 
str(matrices) 
List of 5 
# $ :List of 2 
    # ..$ test_matrix : num [1:67, 1:3] -1.0132 -1.3657 -0.3495 0.6664 0.0762 ... 
    # ..$ train_matrix: num [1:266, 1:3] -0.65 0.797 0.689 0.484 0.682 ... 
# $ :List of 2 
    # ..$ test_matrix : num [1:67, 1:3] 0.484 0.418 -0.622 0.996 0.414 ... 
    # ..$ train_matrix: num [1:266, 1:3] -0.65 0.797 0.689 0.682 0.186 ... 
# $ :List of 2 
    # ..$ test_matrix : num [1:67, 1:3] 0.682 0.812 -1.111 -0.467 0.37 ... 
    # ..$ train_matrix: num [1:266, 1:3] -0.65 0.797 0.689 0.484 0.186 ... 
# $ :List of 2 
    # ..$ test_matrix : num [1:66, 1:3] -0.65 0.797 0.689 0.186 -1.398 ... 
    # ..$ train_matrix: num [1:267, 1:3] 0.484 0.682 0.473 0.812 -1.111 ... 
# $ :List of 2 
    # ..$ test_matrix : num [1:66, 1:3] 0.473 0.212 -2.175 -0.746 1.707 ... 
    # ..$ train_matrix: num [1:267, 1:3] -0.65 0.797 0.689 0.484 0.682 ... 

那么你可以使用lapply得到的结果:

lapply(matrices, function(x) { 
    m <- build_model(x$train_matrix) 
    performance(m, x$test_matrix) 
}) 

编辑:与Wojc相比iech的解决方案:

f_K_fold <- function(Nobs, K=5){ 
    id <- sample(rep(seq.int(K), length.out=Nobs)) 
    l <- lapply(seq.int(K), function(x) list(
     train = which(x!=id), 
     test = which(x==id) 
    )) 
    return(l) 
} 
0

编辑:谢谢您的回答。 我已经发现了以下溶液(http://eric.univ-lyon2.fr/~ricco/tanagra/fichiers/fr_Tanagra_Validation_Croisee_Suite.pdf):

n <- nrow(mydata) 
K <- 5 
size <- n %/% K 
set.seed(5) 
rdm <- runif(n) 
ranked <- rank(rdm) 
block <- (ranked-1) %/% size+1 
block <- as.factor(block) 

然后我使用:

for (k in 1:K) { 
    matrix_train<-matrix[block!=k,] 
    matrix_test<-matrix[block==k,] 
    [Algorithm sequence] 
    } 

以便为每次迭代生成足够的集合。

但是,这种解决方案可以省略一个人进行测试。我不推荐它。

0

不需要创建单独的data.frames /矩阵,你只需要保留一个整数序列,id存储每个折叠的混洗索引。

X <- read.csv('data.csv') 

k = 5 # number of folds 
fold_size <-nrow(X)/k 
indices <- rep(1:k,rep(fold_size,k)) 
id <- sample(indices, replace = FALSE) # random draws without replacement 

log_models <- new.env(hash=T, parent=emptyenv()) 
for (i in 1:k){ 
    train <- X[id != i,] 
    test <- X[id == i,] 
    # run algorithm, e.g. logistic regression 
    log_models[[as.character(i)]] <- glm(outcome~., family="binomial", data=train) 
} 
+0

请注意,当nrow(X)不是k的倍数时,会丢弃一些样本。 – Samuel

0

sperrorest包提供了这种能力。您可以选择随机拆分(partition.cv()),空间拆分(partition.kmeans())或基于因子级别拆分(partition.factor.cv())。后者目前仅在Github版本中提供。

例子:

library(sperrorest) 
data(ecuador) 

## non-spatial cross-validation: 
resamp <- partition.cv(ecuador, nfold = 5, repetition = 1:1) 

# first repetition, second fold, test set indices: 
idx <- resamp[['1']][[2]]$test 

# test sample used in this particular repetition and fold: 
ecuador[idx , ] 

如果你有一个空间数据集(与coords)使用,也可以想像你产生褶皱

# this may take some time... 
plot(resamp, ecuador) 

enter image description here

交叉验证然后可以使用sperrorest()(顺序)或parsperrorest()(并行)进行。