简体   繁体   中英

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

在此处输入图像描述

while in R, the plot looks like: R Plot

在此处输入图像描述

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)?

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)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM