shap / shap

A game theoretic approach to explain the output of any machine learning model.
https://shap.readthedocs.io
MIT License
22.61k stars 3.26k forks source link

Shap with multi-class classification #2509

Closed rohithteja closed 2 months ago

rohithteja commented 2 years ago

I am working with a multi-class classification problem with 4 classes and a random forest model. I was able to retrieve the shape values for the model and make a stacked bar plot showing all 4 classes. The bar plot shows the mean absolute shap values for each feature. Does it make sense to make two stacked bar plots, one using mean positive shap values and other with the mean negative shap values? I am a bit confused on how to do it. Most of the tutorials focus on the mean absolute shap values. Any pointers would be helpful. Thanks a lot!

sorenwacker commented 2 years ago

Can you share some example code? I am stuck with a similar problem. I can get the shap values, however the shape is 3D. Now, I am not sure how to use these values.

rohithteja commented 2 years ago

Sure, I used the following code to create a stacked bar plot with all 4 classes:

explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test)
shap.summary_plot(shap_values, X_test, plot_type="bar",
class_names=['Low','Medium','High','Very High'], class_inds=[0,1,2,3])

Not sure if this would work for your case but you can try removing one class. "shap_values" variable is a list of 4 arrays (one for each class). Hope his helps!

github-actions[bot] commented 5 months ago

This issue has been inactive for two years, so it's been automatically marked as 'stale'.

We value your input! If this issue is still relevant, please leave a comment below. This will remove the 'stale' label and keep it open.

If there's no activity in the next 90 days the issue will be closed.

github-actions[bot] commented 2 months ago

This issue has been automatically closed due to lack of recent activity.

Your input is important to us! Please feel free to open a new issue if the problem persists or becomes relevant again.