treeshap icon indicating copy to clipboard operation
treeshap copied to clipboard

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

Open invain1218 opened this issue 11 months ago • 2 comments

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

invain1218 avatar Feb 17 '25 03:02 invain1218

You mean the SHAP values per category of a probabilistic classification model?

mayer79 avatar Feb 17 '25 14:02 mayer79

@mayer79 Yes

invain1218 avatar Feb 18 '25 05:02 invain1218