Phase 3: Explainability & Transparency · 50 min · Python · SHAP · scikit-learn
SHAP — From Global to Local Explanations
Every prediction has a story. SHAP tells it with math.
What you will learn
- Explain Shapley values and their origin in cooperative game theory
- Use TreeExplainer for tree-based models and KernelExplainer for model-agnostic explanations
- Generate SHAP global summary plots and local waterfall/force plots
- Interpret SHAP values for technical and non-technical stakeholders
Objective
Test whether an explanation is faithful, understandable, decision-relevant, and appropriate for its actual audience.
The Problem
You have a trained model. It's accurate. It's also a black box. You need to answer two questions:
- Global: Which features matter most across all predictions?
- Local: Why was this specific person denied a loan?
SHAP (SHapley Additive exPlanations) answers both. It's based on Shapley values from cooperative game theory — a concept Lloyd Shapley introduced in 1953 for fairly distributing payouts among coalition players. In ML, the "players" are features, the "game" is the model's prediction, and the "payout" is the difference between the prediction and the baseline.
How SHAP works
For a model with features $x_1, x_2, ..., x_n$ producing prediction $f(x)$:
$$f(x) = \phi_0 + \sum_{i=1}^{n} \phi_i$$
Where $\phi_0$ is the baseline (average prediction) and $\phi_i$ is the SHAP value for feature $i$ — its contribution to moving the prediction away from the baseline.
Key properties:
- Efficiency: SHAP values sum to the difference between the prediction and the baseline
- Symmetry: Features with equal impact get equal SHAP values
- Additivity: SHAP values from ensemble models decompose into per-model contributions
TreeExplainer vs. KernelExplainer
| Explainer | Models Supported | Speed | Exact? | When to Use |
|---|
| TreeExplainer | Decision trees, random forests, gradient boosting | Fast | Yes (exact Shapley values) | Always prefer for tree-based models |
| KernelExplainer | Any model | Slow (O(2^n) approximations) | Approximate | Use for non-tree models (SVM, neural nets) |
| DeepExplainer | Neural networks (Keras/TensorFlow) | Medium | Approximate | Use for deep learning models |
| GradientExplainer | Neural networks | Medium | Approximate | Alternative to DeepExplainer |
KernelExplainer performance
KernelExplainer is model-agnostic but computationally expensive. On a dataset with 47 features and 1000 samples, it can take 10+ minutes. Always subsample to 100-200 rows for KernelExplainer. For tree-based models, TreeExplainer is 100x faster and produces exact values — use it whenever possible.
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The Concept, Check Yourself, Key Terms & Next — plus a hands-on lab, quiz, and project artifact.
Create a free account — the opening phases of 29 of 35 courses are free, no credit card.
Browse all courses · View pricing · DeVenture Academy