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

remove uses of predict(reshape = TRUE) for lightgbm models (fixes #40) #41

Merged
merged 3 commits into from
Jul 18, 2022
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
23 changes: 20 additions & 3 deletions R/lightgbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ sort_args <- function(args) {
args
}

# in lightgbm <= 3.3.2, predict() for multiclass classification produced a single
# vector of length num_observations * num_classes, in row-major order
#
# in versions after that release, lightgbm produces a numeric matrix with shape
# [num_observations, num_classes]
#
# this function ensures that multiclass classification predictions are always
# returned as a [num_observations, num_classes] matrix, regardless of lightgbm version
reshape_lightgbm_multiclass_preds <- function(preds, num_rows) {
n_preds_per_case <- length(preds) / num_rows
if (is.vector(preds) && n_preds_per_case > 1) {
preds <- matrix(preds, ncol = n_preds_per_case, byrow = TRUE)
}
preds
}

#' Internal functions
#'
#' Not intended for direct use.
Expand All @@ -268,7 +284,8 @@ sort_args <- function(args) {
#' @export
#' @rdname lightgbm_helpers
predict_lightgbm_classification_prob <- function(object, new_data, ...) {
p <- stats::predict(object$fit, prepare_df_lgbm(new_data), reshape = TRUE, ...)
p <- stats::predict(object$fit, prepare_df_lgbm(new_data), ...)
p <- reshape_lightgbm_multiclass_preds(preds = p, num_rows = nrow(new_data))

if(is.vector(p)) {
p <- tibble::tibble(p1 = 1 - p, p2 = p)
Expand All @@ -294,7 +311,8 @@ predict_lightgbm_classification_class <- function(object, new_data, ...) {
#' @export
#' @rdname lightgbm_helpers
predict_lightgbm_classification_raw <- function(object, new_data, ...) {
stats::predict(object$fit, prepare_df_lgbm(new_data), reshape = TRUE, rawscore = TRUE, ...)
p <- stats::predict(object$fit, prepare_df_lgbm(new_data), rawscore = TRUE, ...)
reshape_lightgbm_multiclass_preds(preds = p, num_rows = nrow(new_data))
}

#' @keywords internal
Expand All @@ -305,7 +323,6 @@ predict_lightgbm_regression_numeric <- function(object, new_data, ...) {
stats::predict(
object$fit,
prepare_df_lgbm(new_data),
reshape = TRUE,
params = list(predict_disable_shape_check = TRUE),
...
)
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-lightgbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ test_that("boost_tree with lightgbm",{
verbose = -1
)

lgbm_preds_4 <- predict(lgbm_fit_4, peng_m_c, reshape = TRUE)
lgbm_preds_4 <-
predict(lgbm_fit_4, peng_m_c) %>%
reshape_lightgbm_multiclass_preds(num_rows = nrow(peng_m_c))

expect_equal(pars_preds_4_mtx, lgbm_preds_4)

Expand Down