Skip to content

How to extract shap values for different levelss in a classification model? #45

@invain1218

Description

@invain1218

Hello!!! Treeshap team,
I would like to know if there exists an extraction method like kernelshap for different levels of SHAP values? Though I already know I could use permshap(X = X_explain) |> shapviz() method to get and plot the SHAP values directly.
Looking forward to your response! 😊

library(shapviz)
library(kernelshap)
library(mlr3)
library(mlr3verse)
library(mlr3learners)
library(mlr3extralearners)
library(mlr3pipelines)

library(treeshap)
library(xgboost)
learner =lrn("classif.xgboost",predict_type="prob")
split = partition(tsk,ratio=0.7)
train = as.data.frame(tsk$data(rows=split$train))
model = learner$train(tsk,split$train)
unified <- unify(model$model, train)
treeshap <- treeshap(unified,  train, verbose = 0)
shap = treeshap$shaps
head(shap)

Petal.Length Petal.Width Sepal.Length Sepal.Width
1 0.1765103 -1.079797 -1.140239 -0.3791572
2 0.1765103 -1.079797 -1.140239 -0.3791572
3 0.1765103 -1.079797 -1.140239 -0.3791572
4 0.1765103 -1.079797 -1.140239 -0.3791572
5 0.1765103 -1.079797 -1.140239 -0.3791572
6 0.1765103 -1.079797 -1.140239 0.4717202

xvars <- tsk$feature_names 
permSHA <- permshap(model, X = train, 
                   feature_names = xvars,
                   predict_type = "prob", bg_X = iris)

Exact permutation SHAP
|==================================================================================================================| 100%

shap = permSHA$S[["setosa"]]
head(shap)
 Petal.Length Petal.Width Sepal.Length  Sepal.Width

[1,] 0.6102708 0.03426293 0.009748821 0.002979857
[2,] 0.6102708 0.03426293 0.009748821 0.002979857
[3,] 0.6102708 0.03426293 0.009748821 0.002979857
[4,] 0.6102708 0.03426293 0.009748821 0.002979857
[5,] 0.6102708 0.03426293 0.009748821 0.002979857
[6,] 0.6069072 0.03673994 0.013476026 -0.005597851

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions