Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate sets for cross-validation

Tags:

r

How to split automatically a matrix using R for 5-fold cross-validation? I actually want to generate the 5 sets of (test_matrix_indices, train matrix_indices).

like image 973
Delphine Avatar asked Sep 13 '11 13:09

Delphine


People also ask

How many iterations does 10 cross-validation takes for model evaluation?

Yes Manas. You repeat the 10-fold cross validation 10 times and take the mean.

What is cross-validation generator?

The cross-validation generator returns an iterable of length n_folds , each element of which is a 2-tuple of numpy 1-d arrays (train_index, test_index) containing the indices of the test and training sets for that cross-validation run.


1 Answers

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)
}
like image 105
Wojciech Sobala Avatar answered Oct 12 '22 01:10

Wojciech Sobala