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

[R-package] added argument eval_train_metric to lgb.cv() (fixes #4911) #4918

Merged
merged 7 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions R-package/R/lgb.cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ 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 eval_train_metric \code{boolean}, whether to add the cross validation results on the
#' training data. This parameter defaults to \code{FALSE}. Setting it to \code{TRUE}
#' will increase run time.
#' @inheritSection lgb_shared_params Early Stopping
#' @return a trained model \code{lgb.CVBooster}.
#'
Expand Down Expand Up @@ -87,6 +90,7 @@ lgb.cv <- function(params = list()
, callbacks = list()
, reset_data = FALSE
, serializable = TRUE
, eval_train_metric = FALSE
) {

if (nrounds <= 0L) {
Expand Down Expand Up @@ -336,6 +340,9 @@ lgb.cv <- function(params = list()
}

booster <- Booster$new(params = params, train_set = dtrain)
if (isTRUE(eval_train_metric)) {
booster$add_valid(data = dtrain, name = "train")
}
booster$add_valid(data = dtest, name = "valid")
return(
list(booster = booster)
Expand Down
7 changes: 6 additions & 1 deletion R-package/man/lgb.cv.Rd

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

39 changes: 39 additions & 0 deletions R-package/tests/testthat/test_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,45 @@ test_that("lgb.cv() respects showsd argument", {
expect_identical(evals_no_showsd[["eval_err"]], list())
})

test_that("lgb.cv() respects eval_train_metric argument", {
dtrain <- lgb.Dataset(train$data, label = train$label)
params <- list(
objective = "regression"
, metric = "l2"
, min_data = 1L
)
nrounds <- 5L
set.seed(708L)
bst_train <- lgb.cv(
params = params
, data = dtrain
, nrounds = nrounds
, nfold = 3L
, showsd = FALSE
, eval_train_metric = TRUE
)
set.seed(708L)
bst_no_train <- lgb.cv(
params = params
, data = dtrain
, nrounds = nrounds
, nfold = 3L
, showsd = FALSE
, eval_train_metric = FALSE
)
expect_equal(
bst_train$record_evals[["valid"]][["l2"]]
, bst_no_train$record_evals[["valid"]][["l2"]]
)
expect_true("train" %in% names(bst_train$record_evals))
expect_false("train" %in% names(bst_no_train$record_evals))
expect_true(methods::is(bst_train$record_evals[["train"]][["l2"]][["eval"]], "list"))
expect_equal(
length(bst_train$record_evals[["train"]][["l2"]][["eval"]])
, nrounds
)
})

context("lgb.train()")

test_that("lgb.train() works as expected with multiple eval metrics", {
Expand Down