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

LightGBM v4 #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ VignetteBuilder:
knitr
Imports:
stats,
utils,
ggplot2 (>= 3.0.0),
xgboost (>= 0.81.0.0),
data.table (>= 1.12.0),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SHAPforxgboost 0.1.2

* 08/06/2022 Comply to LightGBM's 4.0 `predict()` interface.
* 17/05/2022 Added option `kind = "bar"` to `shap.plot.summary()`.

# SHAPforxgboost 0.1.1
Expand Down
9 changes: 6 additions & 3 deletions R/SHAP_funcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@
shap.values <- function(xgb_model,
X_train){

shap_contrib <- predict(xgb_model,
(X_train),
predcontrib = TRUE)
# New predict() interface for LGB 4
if (inherits(xgb_model, "lgb.Booster") && utils::packageVersion("lightgbm") >= 4) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mayer79 for your consideration.... using >= 4 means that the wrong decision will be made when testing against the current development version of {lightgbm} built from source, where the version is 3.3.2.99.

To work around that, in tidymodels/bonsai#42 I did the following:

using_newer_lightgbm_version <- function(){
    utils::packageVersion("lightgbm") > package_version("3.3.2")
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is clearly better, thanks for pointing it out.

shap_contrib <- predict(xgb_model, X_train, type = "contrib")
} else {
shap_contrib <- predict(xgb_model, X_train, predcontrib = TRUE)
}

# Add colnames if not already there (required for LightGBM)
if (is.null(colnames(shap_contrib))) {
Expand Down