Created
February 22, 2016 17:34
-
-
Save lucianogiuseppe/02a7d6c89653194e561b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
#Example of use | |
k = 7; | |
indici <- 1:length(tLabel) #take index of labels | |
#indici <- sample(indici) #permutation | |
lbl1 = which(tLabel == 0); #samples with label 0 | |
lbl2 = which(tLabel == 1); #samples with label 1 | |
lbl3 = which(tLabel == 2); #samples with label 2 | |
#Make the k-fold for each label | |
indxesLbl1 = kFold(lbl1, k) | |
indxesLbl2 = kFold(lbl2, k) | |
indxesLbl3 = kFold(lbl3, k) | |
#make the | |
fold1 = c(indxesLbl2[[1]], indxesLbl2[[1]], indxesLbl1[[1]) | |
fold1 = c(indxesLbl2[[2]], indxesLbl2[[2]], indxesLbl1[[2]) | |
... | |
###The function | |
#from int. index make K folds | |
kFold <- function(lbl1, k) { | |
lenLbl1 = length(lbl1) | |
parte1 = floor(lenLbl1/k) #part size | |
mod1 = lenLbl1 %% k | |
indici = c() #indexes | |
for (i in seq(1, parte1*k, by=parte1)) { | |
t = i+parte1-1 | |
indici = cbind(indici, list(lbl1[i:t]) ) #make column of indexes | |
} | |
#add the remain elements | |
if(mod1 != 0) { | |
indx = 1 | |
for (i in seq((parte1*k)+1, lenLbl1)) { | |
indici[[indx]] = c(lbl1[[i]], indici[[indx]]) | |
indx = indx + 1 | |
} | |
} | |
return(indici); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment