Skip to content
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

WIP: [R-package] Add support for specifying training indices in lgb.cv() #3989

Closed
wants to merge 10 commits into from
12 changes: 11 additions & 1 deletion R-package/R/lgb.cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ CVBooster <- R6::R6Class(
#' @param callbacks List of callback functions that are applied at each iteration.
#' @param reset_data Boolean, setting it to TRUE (not the default value) will transform the booster model
#' into a predictor model which frees up memory and the original datasets
#' @param train_folds \code{list} specifying which indices to use for training. If \code{NULL}
#' (the default) all indices not specified in \code{folds} will be used for training.
#' @param ... other parameters, see Parameters.rst for more information. A few key parameters:
#' \itemize{
#' \item{\code{boosting}: Boosting type. \code{"gbdt"}, \code{"rf"}, \code{"dart"} or \code{"goss"}.}
Expand Down Expand Up @@ -89,6 +91,7 @@ lgb.cv <- function(params = list()
, early_stopping_rounds = NULL
, callbacks = list()
, reset_data = FALSE
, train_folds = NULL
, ...
) {

Expand Down Expand Up @@ -302,7 +305,14 @@ lgb.cv <- function(params = list()
} else {
test_indices <- folds[[k]]
}
train_indices <- seq_len(nrow(data))[-test_indices]

# Generate train_indices from either the train_folds argument
# or as the opposite of (test)folds argument:
if (!is.null(train_folds)) {
train_indices <- train_folds[[k]]
} else {
train_indices <- seq_len(nrow(data))[-test_indices]
}

# set up test set
indexDT <- data.table::data.table(
Expand Down
4 changes: 4 additions & 0 deletions R-package/man/lgb.cv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.