'SHAP Summary Plot and Mean Values displaying together

Used the following Python code for a SHAP summary_plot:

explainer = shap.TreeExplainer(model2)
shap_values = explainer.shap_values(X_sampled)
shap.summary_plot (shap_values, X_sampled, max_display=X_sampled.shape[1]) 

and got a plot which is something like this: Python Plot

enter image description here

while in R, the plot looks like: R Plot

enter image description here

How can I modify my Python script to include mean (|SHAP value|) corresponding to each feature in the same plot (just like the R output)?



Solution 1:[1]

SHAP plots are a bit tricky to customize unless you're willing to tinker with the source code, but the following will do:

import xgboost
import shap

X, y = shap.datasets.adult()
model = xgboost.XGBClassifier().fit(X, y)
explainer = shap.TreeExplainer(model, X)
shap_values = explainer(X)
feature_names = [
    a + ": " + str(b) for a,b in zip(X.columns, np.abs(shap_values.values).mean(0).round(2))
]

shap.summary_plot(shap_values, X, max_display=X.shape[1], 
                  feature_names=feature_names)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1