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

make lightgbm raw classification predictions backward and forward compatible #42

Merged
merged 1 commit into from
Jul 19, 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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ importFrom(rlang,new_quosure)
importFrom(stats,predict)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
importFrom(utils,packageVersion)
8 changes: 8 additions & 0 deletions R/0_imports.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @importFrom parsnip multi_predict set_mode set_engine fit
#' @importFrom parsnip decision_tree boost_tree rand_forest
#' @importFrom stats predict
#' @importFrom utils packageVersion

# ------------------------------------------------------------------------------

Expand All @@ -24,4 +25,11 @@ utils::globalVariables(
)
)

# ------------------------------------------------------------------------------

# lightgbm had significant breaking changes following release v3.3.2.
# this function is used by patches that make bonsai backward-compatible with
# older lightgbm versions
using_newer_lightgbm_version <- function(){
utils::packageVersion("lightgbm") > package_version("3.3.2")
}
6 changes: 5 additions & 1 deletion R/lightgbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,11 @@ predict_lightgbm_classification_class <- function(object, new_data, ...) {
#' @export
#' @rdname lightgbm_helpers
predict_lightgbm_classification_raw <- function(object, new_data, ...) {
p <- stats::predict(object$fit, prepare_df_lgbm(new_data), rawscore = TRUE, ...)
if (using_newer_lightgbm_version()) {
p <- stats::predict(object$fit, prepare_df_lgbm(new_data), type = "raw", ...)
} else {
p <- stats::predict(object$fit, prepare_df_lgbm(new_data), rawscore = TRUE, ...)
}
reshape_lightgbm_multiclass_preds(preds = p, num_rows = nrow(new_data))
}

Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-lightgbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,13 @@ test_that("boost_tree with lightgbm",{
expect_error_free({
pars_preds_4 <-
predict(pars_fit_4, penguins, type = "prob")
pars_preds_raw_4 <-
predict(pars_fit_4, penguins, type = "raw")
})

expect_equal(nrow(pars_preds_raw_4), nrow(penguins))
expect_equal(ncol(pars_preds_raw_4), 3)

pars_preds_4_mtx <- as.matrix(pars_preds_4)
dimnames(pars_preds_4_mtx) <- NULL

Expand Down Expand Up @@ -202,8 +207,13 @@ test_that("boost_tree with lightgbm",{
expect_error_free({
pars_preds_6 <-
predict(pars_fit_6, penguins, type = "prob")
pars_preds_raw_6 <-
predict(pars_fit_6, penguins, type = "raw")
})

expect_equal(length(pars_preds_raw_6), nrow(penguins))
expect_false(identical(pars_preds_6, pars_preds_raw_6))

pars_preds_6_b <- pars_preds_6$.pred_male

peng_y_b <- peng$sex
Expand Down