Local explainability with SHAP

Explainable AI in Python

Fouad Trad

Machine Learning Engineer

Global vs. local explainability

Global explainability
  • Overall model behavior
  • Doesn't explain individual instances

Professor explaining the general performance of the classroom to all students.

Local explainability
  • Explains prediction for specific data point
  • Crucial for sensitive applications

Professor explaining to a specific student the reason behind their grade.

1 Images generated by DALL-E
Explainable AI in Python

Heart disease dataset

age sex chest_pain_type blood_pressure ecg_results thalassemia target
52 1 0 125 1 3 0
53 1 0 140 0 3 0
70 1 0 145 1 3 0
61 1 0 148 1 3 0
62 0 0 138 1 2 0

 

knn: KNN classifier predicting risk of heart disease

Explainable AI in Python

Local explainability with SHAP

explainer = shap.KernelExplainer(knn.predict_proba, shap.kmeans(X, 10))

test_instance = X.iloc[0, :]
shap_values = explainer.shap_values(test_instance)
print(shap_values.shape)
(6, 2)
Explainable AI in Python

SHAP waterfall plots

  • Shows how features increase or decrease model's prediction

A waterfall plot that shows multiple features and the contribution of each towards the prediction.

Explainable AI in Python

SHAP waterfall plots

  • Shows how features increase or decrease model's prediction

Same waterfall plot showing the baseline value, which serves as an initial point for the prediction.

Explainable AI in Python

SHAP waterfall plots

  • Shows how features increase or decrease model's prediction

Same waterfall plot showing that in waterfall plots, features pushing to the right increase the prediction while features pushing to the left decrease it.

Explainable AI in Python

Creating waterfall plots

shap.waterfall_plot(

shap.Explanation(
values=shap_values[:,1],
base_values=explainer.expected_value[1],
data=test_instance,
feature_names=X.columns
)
)
Explainable AI in Python

Waterfalls for several instances

Figure displaying four waterfall plots, one for each sample, illustrating how different samples follow distinct patterns to reach the model’s decision.

Explainable AI in Python

Let's practice!

Explainable AI in Python

Preparing Video For Download...