knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" ) set.seed(21)
In the era of complicated classifiers conquering their market, sometimes even the authors of algorithms do not know the exact manner of building a tree ensemble model. The difficulties in models' structures are one of the reasons why most users use them simply like black-boxes. But, how can they know whether the prediction made by the model is reasonable? treeshap
is an efficient answer for this question. Due to implementing an optimized algorithm for tree ensemble models (called TreeSHAP), it calculates the SHAP values in polynomial (instead of exponential) time. Currently, treeshap
supports models produced with xgboost
, lightgbm
, gbm
, ranger
, and randomForest
packages. Support for catboost
is available only in catboost
branch (see why here).
The package is available on CRAN:
install.packages('treeshap')
You can install the latest development version from GitHub using devtools
with:
devtools::install_github('ModelOriented/treeshap')
First of all, let's focus on an example how to represent a xgboost
model as a unified model object:
library(treeshap) library(xgboost) data <- fifa20$data[colnames(fifa20$data) != 'work_rate'] target <- fifa20$target param <- list(objective = "reg:squarederror", max_depth = 6) xgb_model <- xgboost::xgboost(as.matrix(data), params = param, label = target, nrounds = 200, verbose = 0) unified <- unify(xgb_model, data) head(unified$model)
Having the object of unified structure, it is a piece of cake to produce SHAP values for a specific observation. The treeshap()
function requires passing two data arguments: one representing an ensemble model unified representation and one with the observations about which we want to get the explanations. Obviously, the latter one should contain the same columns as data used during building the model.
treeshap1 <- treeshap(unified, data[700:800, ], verbose = 0) treeshap1$shaps[1:3, 1:6]
We can also compute SHAP values for interactions. As an example we will calculate them for a model built with simpler (only 5 columns) data and first 100 observations.
data2 <- fifa20$data[, 1:5] xgb_model2 <- xgboost::xgboost(as.matrix(data2), params = param, label = target, nrounds = 200, verbose = 0) unified2 <- unify(xgb_model2, data2) treeshap_interactions <- treeshap(unified2, data2[1:100, ], interactions = TRUE, verbose = 0) treeshap_interactions$interactions[, , 1:2]
The explanation results can be visualized using shapviz
package, see here.
However, treeshap
also provides 4 plotting functions:
On this plot we can see how features contribute into the prediction for a single observation. It is similar to the Break Down plot from iBreakDown package, which uses different method to approximate SHAP values.
plot_contribution(treeshap1, obs = 1, min_max = c(0, 16000000))
This plot shows us average absolute impact of features on the prediction of the model.
plot_feature_importance(treeshap1, max_vars = 6)
Using this plot we can see, how a single feature contributes into the prediction depending on its value.
plot_feature_dependence(treeshap1, "height_cm")
Simple plot to visualize an SHAP Interaction value of two features depending on their values.
plot_interaction(treeshap_interactions, "height_cm", "overall")
For your convenience, you can now simply use the unify()
function by specifying your model and reference dataset. Behind the scenes, it uses one of the six functions from the .unify()
family (xgboost.unify()
, lightgbm.unify()
, gbm.unify()
, catboost.unify()
, randomForest.unify()
, ranger.unify()
). Even though the objects produced by these functions are identical when it comes to the structure, due to different possibilities of saving and representing the trees among the packages, the usage of these model-specific functions may be slightly different. Therefore, you can use them independently or pass some additional parameters to unify()
.
library(treeshap) library(gbm) x <- fifa20$data[colnames(fifa20$data) != 'work_rate'] x['value_eur'] <- fifa20$target gbm_model <- gbm::gbm( formula = value_eur ~ ., data = x, distribution = "laplace", n.trees = 200, cv.folds = 2, interaction.depth = 2 ) unified_gbm <- unify(gbm_model, x) unified_gbm2 <- gbm.unify(gbm_model, x) # legacy API
Dataset used as a reference for calculating SHAP values is stored in unified model representation object. It can be set any time using set_reference_dataset()
function.
library(treeshap) library(ranger) data_fifa <- fifa20$data[!colnames(fifa20$data) %in% c('work_rate', 'value_eur', 'gk_diving', 'gk_handling', 'gk_kicking', 'gk_reflexes', 'gk_speed', 'gk_positioning')] data <- na.omit(cbind(data_fifa, target = fifa20$target)) rf <- ranger::ranger(target~., data = data, max.depth = 10, num.trees = 10) unified_ranger_model <- unify(rf, data) unified_ranger_model2 <- set_reference_dataset(unified_ranger_model, data[c(1000:2000), ])
Package also implements predict()
function for calculating model's predictions using unified representation.
The complexity of TreeSHAP is $\mathcal{O}(TLD^2)$, where $T$ is the number of trees, $L$ is the number of leaves in a tree, and $D$ is the depth of a tree.
Our implementation works at a speed comparable to the original Lundberg's Python package shap
implementation using C and Python.
The complexity of SHAP interaction values computation is $\mathcal{O}(MTLD^2)$, where $M$ is the number of explanatory variables used by the explained model, $T$ is the number of trees, $L$ is the number of leaves in a tree, and $D$ is the depth of a tree.
Originally, treeshap
also supported the CatBoost models from the catboost
package but due to the lack of this package on CRAN or R-universe (see catboost
issues issues #439, #1846), we decided to remove support from the main version of our package.
However, you can still use the treeshap
implementation for catboost
by installing our package from catboost
branch.
This branch can be installed with:
devtools::install_github('ModelOriented/treeshap@catboost')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.