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] skip integer categorical feature check when building dataset subset (fixes #6412) #6442

Merged
merged 9 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion R-package/R/lgb.Dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ Dataset <- R6::R6Class(

# Check if more categorical features were output over the feature space
data_is_not_filename <- !is.character(private$raw_data)
if (data_is_not_filename && max(private$categorical_feature) > ncol(private$raw_data)) {
from_raw_data <- !is.null(private$raw_data) && is.null(private$used_indices)
if (data_is_not_filename && from_raw_data && max(private$categorical_feature) > ncol(private$raw_data)) {
stop(
"lgb.Dataset.construct: supplied a too large value in categorical_feature: "
, max(private$categorical_feature)
Expand Down
22 changes: 22 additions & 0 deletions R-package/tests/testthat/test_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -3652,6 +3652,28 @@ test_that("lgb.cv() only prints eval metrics when expected to", {
)
})

test_that("lgb.cv() works with an already constructed dataset with integer categoricals", {
jameslamb marked this conversation as resolved.
Show resolved Hide resolved
data("mtcars")
y <- mtcars$mpg
x <- as.matrix(mtcars[, -1L])
categorical_feature <- which(names(mtcars) %in% c("cyl", "vs", "am", "gear", "carb")) - 1L
dtrain <- lgb.Dataset(
data = x
, label = y
, categorical_feature = categorical_feature
, free_raw_data = TRUE
, params = list(num_threads = .LGB_MAX_THREADS)
)
dtrain$construct()
params <- list(
objective = "regression"
, num_leaves = 2L
, verbose = .LGB_VERBOSITY
, num_threads = .LGB_MAX_THREADS
)
lgb.cv(params = params, data = dtrain, nrounds = 1L)
jameslamb marked this conversation as resolved.
Show resolved Hide resolved
})

test_that("lightgbm() changes objective='auto' appropriately", {
# Regression
data("mtcars")
Expand Down