Koen
Koen

Reputation: 395

How to show feature values in shap waterfall plot?

Looking into the shap library, I came across this question, in which the answer showcased the waterfall plot, neat! Looking at some of the official examples here and here I notice the plots also showcase the value of the features.

The shap package contains both shap.waterfall_plot and shap.plots.waterfall, trying both on a Random Forest trained on the Iris dataset gave the same results (see one code and image example below)

for which_class in y.unique():
display(
    shap.waterfall_plot(shap.Explanation(values=shap_values[int(which_class)][idx], 
                                         base_values=explainer.expected_value[int(which_class)], 
                                         feature_names=X_test.columns.tolist())
                       )
)

In which idx indicates a sample in the test set I'm trying to explain. The code generates the following plot for one of the classes: enter image description here

How can I get the plot to also display the feature values? I didn't see any additional arguments I could pass to the plot method

Any help is greatly appreciated!

Upvotes: 3

Views: 7886

Answers (1)

Koen
Koen

Reputation: 395

Found it!

the shap.Explanation method has an argument where you can pass the data. See edited example below

for which_class in y.unique():
display(
    shap.waterfall_plot(shap.Explanation(values=shap_values[int(which_class)][row], 
                                         base_values=explainer.expected_value[int(which_class)], 
                                         data=X_test.iloc[row],  # added this line
                                         feature_names=X_test.columns.tolist())
                       )
)

enter image description here (don't mind the slight difference in the contributions of the features compared to the image uploaded in the question, there's probably something off in setting the random seed)

Upvotes: 5

Related Questions