-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[R-package] Accept factor labels and use their levels (#5341)
- Loading branch information
1 parent
9713ff4
commit c676a7e
Showing
9 changed files
with
313 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -63,4 +63,4 @@ Imports: | |
utils | ||
SystemRequirements: | ||
C++11 | ||
RoxygenNote: 7.2.1 | ||
RoxygenNote: 7.2.3 |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
DataProcessor <- R6::R6Class( | ||
classname = "lgb.DataProcessor", | ||
public = list( | ||
factor_levels = NULL, | ||
|
||
process_label = function(label, objective, params) { | ||
|
||
if (is.character(label)) { | ||
label <- factor(label) | ||
} | ||
|
||
if (is.factor(label)) { | ||
|
||
self$factor_levels <- levels(label) | ||
if (length(self$factor_levels) <= 1L) { | ||
stop("Labels to predict is a factor with <2 possible values.") | ||
} | ||
|
||
label <- as.numeric(label) - 1.0 | ||
out <- list(label = label) | ||
if (length(self$factor_levels) == 2L) { | ||
if (objective == "auto") { | ||
objective <- "binary" | ||
} | ||
if (!(objective %in% .BINARY_OBJECTIVES())) { | ||
stop("Two-level factors as labels only allowed for objective='binary' or objective='auto'.") | ||
} | ||
} else { | ||
if (objective == "auto") { | ||
objective <- "multiclass" | ||
} | ||
if (!(objective %in% .MULTICLASS_OBJECTIVES())) { | ||
stop( | ||
sprintf( | ||
"Factors with >2 levels as labels only allowed for multi-class objectives. Got: %s (allowed: %s)" | ||
, objective | ||
, toString(.MULTICLASS_OBJECTIVES()) | ||
) | ||
) | ||
} | ||
data_num_class <- length(self$factor_levels) | ||
params <- lgb.check.wrapper_param( | ||
main_param_name = "num_class" | ||
, params = params | ||
, alternative_kwarg_value = data_num_class | ||
) | ||
if (params[["num_class"]] != data_num_class) { | ||
warning( | ||
sprintf( | ||
"Found num_class=%d in params, but 'label' is a factor with %d levels. 'num_class' will be ignored." | ||
, params[["num_class"]] | ||
, data_num_class | ||
) | ||
) | ||
params$num_class <- data_num_class | ||
} | ||
} | ||
out$objective <- objective | ||
out$params <- params | ||
return(out) | ||
|
||
} else { | ||
|
||
label <- as.numeric(label) | ||
if (objective == "auto") { | ||
objective <- "regression" | ||
} | ||
out <- list( | ||
label = label | ||
, objective = objective | ||
, params = params | ||
) | ||
return(out) | ||
|
||
} | ||
}, | ||
|
||
process_predictions = function(pred, type) { | ||
if (NROW(self$factor_levels)) { | ||
if (type == "class") { | ||
pred <- as.integer(pred) + 1L | ||
attributes(pred)$levels <- self$factor_levels | ||
attributes(pred)$class <- "factor" | ||
} else if (type %in% c("response", "raw")) { | ||
if (is.matrix(pred) && ncol(pred) == length(self$factor_levels)) { | ||
colnames(pred) <- self$factor_levels | ||
} | ||
} | ||
} | ||
|
||
return(pred) | ||
} | ||
) | ||
) |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.