SHAP Values
Fairly dividing each prediction's credit
SHAP decomposes each prediction into additive per-feature Shapley values — how much each feature pushed the prediction away from the baseline. Consistent, locally accurate, and the default for explaining tabular models.
What you will learn
- Explain Shapley values and additive decomposition
- Read SHAP summary and waterfall plots
- Recognize approximation caveats at scale
What is this?
SHAP explains a prediction as a baseline (average prediction) plus per-feature additive contributions: predicted = base + Σ φⱼ, where φⱼ is the Shapley value of feature j — the fair average of its marginal contribution over all feature subsets/orderings. It is the only additive attribution with consistency guarantee, so it agrees with humans' intuition of 'pushed up vs down'. Computation is exact for trees and the kernel SHAP approximation for general models.
Why do you need it?
- Consistency: a model that relies more on a feature can't give it a smaller φ — rare among explainers.
- Locally accurate: contributions sum exactly to the prediction vs baseline.
- One framework for texts, images and tables via different backends.
- The de-facto standard for tabular ML auditing and reporting.
Mental model
Handing out a bonus fairly across a team
Shapley value attribution
The bonus per teammate measures how much each contributed across every possible grouping — alone, in pairs, with the whole team. SHAP does the same for features: how much each pushed the prediction, averaged over all coalitions, guaranteeing the parts sum to the whole.
How it works
- Define baseline φ₀ = E[f(x)] over the background dataset.
- For feature j, compare f on subsets that include vs exclude j, averaged over all subsets → Shapley φⱼ.
- Enforce efficiency: Σ φⱼ = f(x) − baseline — the waterfall closes.
- Fast paths: TreeSHAP (exact for trees), linear SHAP (analytic), gradient SHAP for NN, KernelSHAP general.
- Aggregate: |φ| averages → per-feature global importance; spatial/cross rows → dependence plots.
A SHAP waterfall: base + pushes = prediction.
In production
- Adverse-action letters: top-3 negative contributions per rejected application.
- Model audits: SHAP summary + dependence plots shipped with each model version.
- Feature-aware monitoring: track average |φ| per feature over time — drift early warnings.
Best practices
- Use TreeSHAP for tabular tree models; default is exact and fast.
- Choose a background sample that reflects production distribution (e.g. recent 1000 rows).
- Plot summary + dependence + waterfall together; one metric alone over-trusts a single view.
- For correlated features, read grouped SHAP (sum over the correlated block).
Common mistakes
- Interpreting φ as causality or as what would happen if you changed the feature.
- Running KernelSHAP on 10k-feature text and calling the noisy output meaningful.
- Editing one feature while holding others fixed and expecting φ to track — SHAP is counterfactual-consistent but not simplistic.
- Forgetting that baseline choice changes every value; report it.
Common issues
Waterfall doesn't close (Σ φ ≠ f(x) − base)Approximated backend (KernelSHAP) under-sampled or baseline misspecified
Increase samples, verify background matches production, and use TreeSHAP when the model allows.
SHAP values look random run to runToo few background samples for KernelSHAP
Grow background size, fix seed, or switch backends with a deterministic path.
Negative importance for obviously useful featureCollinearity redistributes credit; lone-feature SHAP hides the group
Report grouped/correlated feature contributions and pair with permutation importance.
Debugging tips
- Verify efficiency numerically: f(x) − base ≈ Σ φ on a handful of rows — if not, something's wrong in plumbing.
- Two explainers (TreeSHAP vs PermutationSHAP vs KernelSHAP) should broadly agree; big gaps = approximation problems.
- SHAP value near zero for a leak-y feature usually means the leak is redundant, not absent.
- Plot |φ| vs feature value as a dependence plot — linear-ish bands suggest additive relationships.
Interview checkpoint
Hands-on lab
- Train a gradient-boosted tree and compute TreeSHAP values
- Plot summary and a waterfall for one prediction
- Verify the sums close and interpret the attribution
Open a Model with SHAP
40 min · advanced
Quick quiz
The sum of a sample's SHAP values equals:
SHAP's consistency guarantee means a model relying more on a feature must give it a non-smaller value.