SHAP Values
In Lesson 2 you ranked features for a black-box model by shuffling them. That answered the product lead's global question: which features drive this model overall? It said nothing about one customer.
Back in Lesson 1 the retention rep had a local question: why was Ravi flagged at 0.87? We answered it with a waterfall, a baseline plus one push per feature, but we never said where those pushes come from, or why they are the fair pushes to show. This lesson fixes that. SHAP is a principled recipe, borrowed from game theory, for splitting a single prediction into per-feature contributions that add up to it exactly, and it works on any model.
By the end you will be able to:
- Explain SHAP as a fair split of one prediction into per-feature contributions that sum exactly to it
- Derive a Shapley value as a feature's average marginal contribution over every join order, and compute one in R
- Read a SHAP waterfall, and turn many local explanations into a global importance
- Name SHAP's honest limits: its cost, correlated features, and why it explains the model, not the world
Prerequisites: you have done Lesson 1: Global vs Local Explanations and Lesson 2: Permutation and Drop-Column Importance, you can fit and use a model in R such as a random forest, and you know what a feature, a prediction, and a baseline are.
One prediction, a payout to divide
Our churn model scores Ravi at a 0.87 risk of leaving. The average customer scores about 0.30. That average is the baseline: the model's best guess before it looks at anything specific about Ravi. So something about Ravi pushed his score 0.57 above the baseline, and a local explanation has to say which features did the pushing, and by how much.
Write \(\hat{f}(x)\) for the model's prediction on one customer \(x\), \(\phi_0\) for the baseline (the average prediction), and \(\phi_j\) for the contribution of feature \(j\) to this prediction. A complete local explanation splits the prediction like this:
\[ \hat{f}(x) = \phi_0 + \sum_{j=1}^{p} \phi_j, \]
where \(p\) is the number of features. In words: baseline plus every feature's push equals the prediction, with nothing left over. That "adds up exactly" property has a name, efficiency (or local accuracy), and it is the whole reason a waterfall is an explanation rather than a loose ranking. The gap it must divide up here is \(0.87 - 0.30 = 0.57\).
The open question is the hard part: how do we set each \(\phi_j\) fairly? Especially when features interact or overlap, as the correlated twins in Lesson 2 did.