Skip to content

Commit 09be1a0

Browse files
authored
Merge pull request #53 from mayer79/prepare-cran-release
Prepare CRAN release
2 parents 6bb80d2 + cf9b082 commit 09be1a0

27 files changed

Lines changed: 97 additions & 53 deletions

‎.Rbuildignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ vignettes/
1313
pkgdown/
1414
^LICENSE$
1515
^CONTRIBUTING.md
16-
^NEWS.md
1716
DALEXpiramide
1817
^codecov\.yml$
1918
^\.github$
2019
^tox.ini$
2120
^pkgdown$
2221
^cran-comments\.md$
2322
^CRAN-SUBMISSION$
23+
^revdep$

‎NEWS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# treeshap (development version)
22

33
# treeshap 0.4.0
4+
* Cleanup in #52 (https://github.com/ModelOriented/treeshap/pull/52). Thanks @kapsner
45
* Fixed xgboost api and add depends R>=4.1.0 as in #51 (https://github.com/ModelOriented/treeshap/pull/51). Thanks @kapsner
56
* Fix ranger.unify for probability forests as in #43 (https://github.com/ModelOriented/treeshap/pull/43). Thanks @ck37
67
* Added support for GPBoost as in #41 (https://github.com/ModelOriented/treeshap/pull/41). Thanks @fabsig

‎R/plot_contribution.R‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@
3535
#' y = target,
3636
#' objective = "reg:squarederror",
3737
#' max_depth = 3,
38-
#' nrounds = 20
38+
#' nrounds = 20,
39+
#' nthreads = 1
3940
#' )
4041
#' unified_model <- xgboost.unify(xgb_model, as.matrix(data))
4142
#' x <- head(data, 1)
4243
#' shap <- treeshap(unified_model, x)
43-
#' plot_contribution(shap, 1, min_max = c(0, 120000000))
44+
#' plot_contribution(shap, 1, min_max = c(-5e5, 1.3e8))
4445
#' }}
45-
#'
46+
#'
4647
plot_contribution <- function(treeshap,
4748
obs = 1,
4849
max_vars = 5,
@@ -154,7 +155,7 @@ plot_contribution <- function(treeshap,
154155
geom_errorbarh(data = df[-c(nrow(df), if (explain_deviation) nrow(df) - 1), ],
155156
aes(xmax = position - 0.85,
156157
xmin = position + 0.85,
157-
y = cumulative), height = 0,
158+
y = cumulative), width = 0,
158159
color = "#371ea3") +
159160
geom_rect(alpha = 0.9) +
160161
if (!explain_deviation) (geom_hline(data = df[df$variable == "intercept", ],

‎R/plot_feature_dependence.R‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
#' y = target,
3030
#' objective = "reg:squarederror",
3131
#' max_depth = 3,
32-
#' nrounds = 20
32+
#' nrounds = 20,
33+
#' nthreads = 1
3334
#' )
3435
#' unified_model <- xgboost.unify(xgb_model, as.matrix(data))
3536
#' x <- head(data, 100)

‎R/plot_feature_importance.R‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
#' y = target,
3333
#' objective = "reg:squarederror",
3434
#' max_depth = 3,
35-
#' nrounds = 20
35+
#' nrounds = 20,
36+
#' nthreads = 1
3637
#' )
3738
#' unified_model <- xgboost.unify(xgb_model, as.matrix(data))
3839
#' shaps <- treeshap(unified_model, as.matrix(head(data, 3)))

‎R/plot_interaction.R‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
#' y = target,
3131
#' objective = "reg:squarederror",
3232
#' max_depth = 5,
33-
#' nrounds = 10
33+
#' nrounds = 10,
34+
#' nthreads = 1
3435
#' )
3536
#' unified_model2 <- xgboost.unify(xgb_model2, data)
3637
#' inters <- treeshap(unified_model2, as.matrix(data[1:50, ]), interactions = TRUE)

‎R/treeshap.R‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,41 @@
2828
#' library(xgboost)
2929
#' data <- fifa20$data[colnames(fifa20$data) != 'work_rate']
3030
#' target <- fifa20$target
31-
#'
31+
#'
3232
#' # calculating simple SHAP values
3333
#' xgb_model <- xgboost::xgboost(
3434
#' x = as.matrix(data),
3535
#' y = target,
3636
#' objective = "reg:squarederror",
3737
#' max_depth = 3,
38-
#' nrounds = 20
38+
#' nrounds = 20,
39+
#' nthreads = 1
3940
#' )
4041
#' unified_model <- xgboost.unify(xgb_model, as.matrix(data))
4142
#' treeshap1 <- treeshap(unified_model, head(data, 3))
4243
#' plot_contribution(treeshap1, obs = 1)
4344
#' treeshap1$shaps
44-
#'
45+
#'
4546
#' # It's possible to calcualte explanation over different part of the data set
46-
#'
47+
#'
4748
#' unified_model_rec <- set_reference_dataset(unified_model, data[1:1000, ])
4849
#' treeshap_rec <- treeshap(unified_model, head(data, 3))
4950
#' plot_contribution(treeshap_rec, obs = 1)
50-
#'
51+
#'
5152
#' # calculating SHAP interaction values
5253
#' xgb_model2 <- xgboost::xgboost(
5354
#' x = as.matrix(data),
5455
#' y = target,
5556
#' objective = "reg:squarederror",
5657
#' max_depth = 7,
57-
#' nrounds = 10
58+
#' nrounds = 10,
59+
#' nthreads = 1
5860
#' )
5961
#' unified_model2 <- xgboost.unify(xgb_model2, as.matrix(data))
6062
#' treeshap2 <- treeshap(unified_model2, head(data, 3), interactions = TRUE)
6163
#' treeshap2$interactions
6264
#' }}
63-
#'
65+
#'
6466
treeshap <- function(unified_model, x, interactions = FALSE, verbose = TRUE) {
6567
UseMethod("treeshap", unified_model)
6668
}

‎R/unify_gpboost.R‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#' \code{\link{gbm.unify}} for \code{\link[gbm:gbm]{GBM models}}
2020
#'
2121
#' \code{\link{xgboost.unify}} for \code{\link[xgboost:xgboost]{XGBoost models}}
22-
#'
22+
#'
2323
#' \code{\link{lightgbm.unify}} for \code{\link[lightgbm:lightgbm]{LightGBM models}}
2424
#'
2525
#' \code{\link{ranger.unify}} for \code{\link[ranger:ranger]{ranger models}}
@@ -40,7 +40,7 @@
4040
#' x <- gpboost::gpb.Dataset(sparse_data, label = as.matrix(data[,ncol(data)]))
4141
#' gpb_data <- gpboost::gpb.Dataset.construct(x)
4242
#' gpb_model <- gpboost::gpboost(data = gpb_data, params = param_gpb,
43-
#' verbose = -1, num_threads = 0)
43+
#' verbose = -1, num_threads = 1)
4444
#' unified_model <- gpboost.unify(gpb_model, sparse_data)
4545
#' shaps <- treeshap(unified_model, data[1:2, ])
4646
#' plot_contribution(shaps, obs = 1)
@@ -90,26 +90,26 @@ gpboost.unify <- function(gpb_model, data, recalculate = FALSE) {
9090
df <- df[, c("tree_index", "split_index", "split_feature", "Decision.type", "threshold", "Yes", "No", "Missing", "split_gain", "internal_count")]
9191
colnames(df) <- c("Tree", "Node", "Feature", "Decision.type", "Split", "Yes", "No", "Missing", "Prediction", "Cover")
9292
attr(df, "sorted") <- NULL
93-
93+
9494
ID <- paste0(df$Node, "-", df$Tree)
9595
df$Yes <- match(paste0(df$Yes, "-", df$Tree), ID)
9696
df$No <- match(paste0(df$No, "-", df$Tree), ID)
9797
df$Missing <- match(paste0(df$Missing, "-", df$Tree), ID)
98-
98+
9999
# Here we lose "Quality" information
100100
df$Prediction[!is.na(df$Feature)] <- NA
101-
101+
102102
feature_names <- jsonlite::fromJSON(gpb_model$dump_model())$feature_names
103103
data <- data[,colnames(data) %in% feature_names]
104-
104+
105105
ret <- list(model = as.data.frame(df), data = as.data.frame(data), feature_names = feature_names)
106106
class(ret) <- "model_unified"
107107
attr(ret, "missing_support") <- TRUE
108108
attr(ret, "model") <- "gpboost"
109-
109+
110110
if (recalculate) {
111111
ret <- set_reference_dataset(ret, as.data.frame(data))
112112
}
113-
113+
114114
return(ret)
115115
}

‎R/unify_lightgbm.R‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#' x <- lightgbm::lgb.Dataset(sparse_data, label = as.matrix(data[,ncol(data)]))
3838
#' lgb_data <- lightgbm::lgb.Dataset.construct(x)
3939
#' lgb_model <- lightgbm::lightgbm(data = lgb_data, params = param_lgbm,
40-
#' verbose = -1, num_threads = 0)
40+
#' verbose = -1, num_threads = 1)
4141
#' unified_model <- lightgbm.unify(lgb_model, sparse_data)
4242
#' shaps <- treeshap(unified_model, data[1:2, ])
4343
#' plot_contribution(shaps, obs = 1)

‎R/unify_xgboost.R‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
#' y = target,
3232
#' objective = "reg:squarederror",
3333
#' max_depth = 3,
34-
#' nrounds = 20
34+
#' nrounds = 20,
35+
#' nthreads = 1
3536
#' )
3637
#' unified_model <- xgboost.unify(xgb_model, as.matrix(data))
3738
#' shaps <- treeshap(unified_model, data[1:2,])

0 commit comments

Comments
 (0)