Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
###
#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
You can’t perform that action at this time.