Repository navigation
Fix validation and fold assignment in get_k_folds #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,49 @@ | ||
| # K folds cross validation is essential for machine learning | ||
| # createFolds function in package caret is easy to use | ||
| # here we write our own function | ||
| # Create randomized k-fold cross-validation assignments. | ||
| # | ||
| # y: vector whose observations should be divided into folds | ||
| # k: number of folds | ||
| # isList: if TRUE, return observation indices grouped by fold; | ||
| # if FALSE, return one fold ID per observation | ||
| # seed: random seed for reproducibility | ||
|
|
||
| get_k_folds<-function(y = c(),k = 10, isList = TRUE, seed = 123){ | ||
| get_k_folds <- function( | ||
| y = c(), | ||
| k = 10, | ||
| isList = TRUE, | ||
| seed = 123 | ||
| ) { | ||
| n <- length(y) | ||
|
|
||
| if (n == 0) { | ||
| stop("y must contain at least one observation") | ||
| } | ||
|
|
||
| if (length(k) != 1 || | ||
| !is.numeric(k) || | ||
| is.na(k) || | ||
| k != as.integer(k)) { | ||
| stop("k must be a positive integer") | ||
| } | ||
|
|
||
| if (k < 2 || k > n) { | ||
| stop("k must be between 2 and length(y)") | ||
| } | ||
|
|
||
| if (length(isList) != 1 || !is.logical(isList) || is.na(isList)) { | ||
| stop("isList must be TRUE or FALSE") | ||
| } | ||
|
|
||
| set.seed(seed) | ||
| folds<-sample(1:length(y), length(y)) | ||
| every_n<-ceiling(length(y)/k) | ||
| matFolds<-suppressWarnings(matrix(folds, ncol=every_n, byrow = T)) | ||
|
|
||
| if(isList){ | ||
| value<-NULL | ||
| rownames(matFolds)<-paste("Folds",1:k,sep="") | ||
| value<-lapply(1:k, function(x){ | ||
| if(x == k){ | ||
| return(matFolds[x,][1:(length(y)-every_n*(k-1))]) | ||
| }else{ | ||
| return(matFolds[x,]) | ||
| } | ||
| }) | ||
| }else{ | ||
| value<-c() | ||
| for(i in 1:length(y)){ | ||
| value[i]<-ceiling(i/every_n) | ||
| } | ||
|
|
||
| # Randomly assign every observation to a fold. | ||
| # Fold sizes differ by at most one observation. | ||
| fold_ids <- sample(rep(seq_len(k), length.out = n)) | ||
|
|
||
| if (isList) { | ||
| return(lapply(seq_len(k), function(fold) { | ||
| which(fold_ids == fold) | ||
| })) | ||
|
siriak marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| return(value) | ||
| } | ||
| fold_ids | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.