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] allow use of categorical_features in Dataset when raw data does not have column names (fixes #4374) #5184

Merged
merged 5 commits into from
Apr 30, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions R-package/R/lgb.Dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,31 +156,32 @@ Dataset <- R6::R6Class(
# Check for character name
if (is.character(private$categorical_feature)) {

cate_indices <- as.list(match(private$categorical_feature, private$colnames) - 1L)
cate_indices <- as.list(match(private$categorical_feature, private$colnames) - 1L)
jmoralez marked this conversation as resolved.
Show resolved Hide resolved

# Provided indices, but some indices are missing?
if (sum(is.na(cate_indices)) > 0L) {
stop(
"lgb.self.get.handle: supplied an unknown feature in categorical_feature: "
, sQuote(private$categorical_feature[is.na(cate_indices)])
)
}

# Provided indices, but some indices are missing?
if (sum(is.na(cate_indices)) > 0L) {
stop(
"lgb.self.get.handle: supplied an unknown feature in categorical_feature: "
, sQuote(private$categorical_feature[is.na(cate_indices)])
)
}
} else {

} else {
# Check if more categorical features were output over the feature space
data_is_matrix <- is.matrix(private$raw_data) || methods::is(private$raw_data, "dgCMatrix")
if (data_is_matrix && max(private$categorical_feature) > ncol(private$raw_data)) {
jmoralez marked this conversation as resolved.
Show resolved Hide resolved
stop(
"lgb.self.get.handle: supplied a too large value in categorical_feature: "
, max(private$categorical_feature)
, " but only "
, ncol(private$raw_data)
, " features"
)
}

# Check if more categorical features were output over the feature space
if (max(private$categorical_feature) > length(private$colnames)) {
stop(
"lgb.self.get.handle: supplied a too large value in categorical_feature: "
, max(private$categorical_feature)
, " but only "
, length(private$colnames)
, " features"
)
}

# Store indices as [0, n-1] indexed instead of [1, n] indexed
cate_indices <- as.list(private$categorical_feature - 1L)
# Store indices as [0, n-1] indexed instead of [1, n] indexed
cate_indices <- as.list(private$categorical_feature - 1L)

}

Expand Down
16 changes: 16 additions & 0 deletions R-package/tests/testthat/test_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,19 @@ test_that("lgb.Dataset$get_feature_num_bin() works", {
actual_num_bins <- sapply(1L:5L, ds$get_feature_num_bin)
expect_identical(actual_num_bins, expected_num_bins)
})

test_that("lgb.Dataset can be constructed with categorical features and without colnames", {
# check that dataset can be constructed
raw_mat <- data.matrix(rep(c(0L, 1L), 50L))
jmoralez marked this conversation as resolved.
Show resolved Hide resolved
ds <- lgb.Dataset(raw_mat, categorical_feature = 1L)$construct()
sparse_mat <- Matrix::Matrix(raw_mat, sparse = TRUE)
expect_true(methods::is(sparse_mat, "dgCMatrix"))
jmoralez marked this conversation as resolved.
Show resolved Hide resolved
ds2 <- lgb.Dataset(sparse_mat, categorical_feature = 1L)$construct()
# check that the column names are NULL
expect_null(ds$.__enclos_env__$private$colnames)
expect_null(ds2$.__enclos_env__$private$colnames)
# check for error when index is greater than the number of columns
expect_error({
lgb.Dataset(raw_mat, categorical_feature = 2L)$construct()
}, regexp = "supplied a too large value in categorical_feature: 2 but only 1 features")
})