SHAP Importance for Ranger in R

1.9k views Asked by At

Having a binary Classification problem: how would be possible to get the Shap Contribution for variables for a Ranger model?

Sample data:

library(ranger)
library(tidyverse)

# Binary Dataset
df <- iris
df$Target <- if_else(df$Species == "setosa",1,0)
df$Species <- NULL

# Train Ranger Model
model <- ranger(
  x = df %>%  select(-Target),
  y = df %>%  pull(Target))

I have tried with several libraries(DALEX, shapr, fastshap, shapper) but I didnt get any solution.

I wish getting some result like SHAPforxgboost for xgboost like:

  • the output of shap.values which is the shap contribution of variables
  • the shap.plot.summary
2

There are 2 answers

0
Carles On BEST ANSWER

Good Morning!, According to what I have found, you can use ranger() with fastshap() as following:

library(fastshap)
library(ranger)
library(tidyverse)
data(iris)
# Binary Dataset
df <- iris
df$Target <- if_else(df$Species == "setosa",1,0)
df$Species <- NULL
x <- df %>%  select(-Target)
# Train Ranger Model
model <- ranger(
  x = df %>%  select(-Target),
  y = df %>%  pull(Target))
# Prediction wrapper
pfun <- function(object, newdata) {
  predict(object, data = newdata)$predictions
}

# Compute fast (approximate) Shapley values using 10 Monte Carlo repetitions
system.time({  # estimate run time
  set.seed(5038)
  shap <- fastshap::explain(model, X = x, pred_wrapper = pfun, nsim = 10)
})

# Load required packages
library(ggplot2)
theme_set(theme_bw())

# Aggregate Shapley values
shap_imp <- data.frame(
  Variable = names(shap),
  Importance = apply(shap, MARGIN = 2, FUN = function(x) sum(abs(x)))
)

Then for example, for variable importance, you can do:

# Plot Shap-based variable importance
ggplot(shap_imp, aes(reorder(Variable, Importance), Importance)) +
  geom_col() +
  coord_flip() +
  xlab("") +
  ylab("mean(|Shapley value|)")

enter image description here

Also, if you want individual predictions, the following is possible:

# Plot individual explanations
expl <- fastshap::explain(model, X = x ,pred_wrapper = pfun, nsim = 10, newdata = x[1L, ])
autoplot(expl, type = "contribution")

All this information has been found in here, and there is more to it: https://bgreenwell.github.io/fastshap/articles/fastshap.html Check the link and solve your doubts ! :)

enter image description here

1
Michael M On

I launched two R packages to perform such tasks: One is "kernelshap" (crunching), the other one is "shapviz" (plotting).

library(randomForest)
library(kernelshap)
Ilibrary(shapviz)

set.seed(1)
fit <- randomForest(Sepal.Length ~ ., data = iris,)

# bg_X is usually a small (50-200 rows) subset of the data

# Step 1: Calculate Kernel SHAP values
s <- kernelshap(fit, iris[-1], bg_X = iris)

# Step 2: Turn them into a shapviz object
sv <- shapviz(s)

# Step 3: Gain insights...
sv_importance(sv, show_numbers = TRUE)
sv_dependence(sv, v = "Petal.Length", color_var = "auto")

enter image description here enter image description here