Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
invain1218 opened this issue Feb 17, 2025 · 2 comments

Comments

@invain1218
Copy link

invain1218 commented Feb 17, 2025

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

@mayer79
Copy link
Contributor

mayer79 commented Feb 17, 2025

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

@invain1218
Copy link
Author

@mayer79 Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants