6.3.2 · D3Interpretability & Explainability

Worked examples — Feature attribution (SHAP, LIME)

2,480 words11 min readBack to topic

This is the hands-on companion to Feature attribution (SHAP, LIME). The parent note built the ideas: LIME's local linear fit and SHAP's fair Shapley credit. Here we grind through every case class you can meet — the friendly ones and the traps.

Before we start, one promise from the contract: no symbol appears before it is earned. We reuse only three things from the parent, and we restate each in plain words the first time it shows up:

Recall The three symbols we carry over
  • ::: the model's prediction for one input (a single number, e.g. a probability like ).
  • ::: the base value — the average prediction if you knew nothing about the instance. Written , read "expected value of over all inputs", i.e. the plain average output.
  • ::: the Shapley value of feature — the fair amount of the gap that we hand to feature .

The scenario matrix

Every attribution problem falls into one of these cells. The examples below are labelled by cell letter so you can see the whole grid is covered.

Cell Case class What makes it tricky Example
A Positive push (feature raises output) sign of is Ex 1
B Negative push (feature lowers output) sign of is Ex 2
C Efficiency check (do the pieces sum?) must equal exactly Ex 3
D Dummy feature (zero contribution) degenerate: Ex 4
E Interaction / order dependence marginal gain differs per ordering Ex 5
F LIME local linear fit (tabular) weighted regression, sign of weights Ex 6
G LIME instability (limiting behaviour) too-few-samples variance blow-up Ex 7
H Real-world word problem translate a story into 's Ex 8
I Exam twist (base value confusion) mixing with a feature's Ex 9

We work SHAP first (cells A–E, I), then LIME (F, G), then a story (H).

Look at the bar picture above: this is the mental model for every SHAP example. Start at the base value (grey), then each feature's is an arrow pushing the running total right (positive, blue) or left (negative, orange), and you must land exactly on .


Warm-up: a tiny 2-feature model we can compute by hand

To do Shapley values exactly (not sampled), we need a model small enough to write down. Let features be , each either or . Define the model on all four possible inputs:

What is ? The average over all four inputs (each equally likely):

Why the average? is "the prediction before you look at any feature" — so you average over everything you don't yet know.

We will attribute the gap between features and .


SHAP examples

The waterfall above visualises Ex 3: base . If the last bar doesn't touch , you have a bug.


LIME examples

LIME does not use Shapley weights. Instead it fits a weighted linear model near . Symbols we earn now:


Real-world & exam cells


  • Global cousins of these local scores: global vs local interpretability and permutation importance.
  • TreeSHAP exploits structure explained in tree-ensemble interpretation.
  • Attribution's sibling that asks "what change flips the decision?" — counterfactual explanations.
  • Attention scores are a different kind of attribution — see attention mechanisms.
Recall Self-test

Why must SHAP sum to and not to ? ::: The explain the deviation from the base value ; the efficiency axiom gives . In the AND game (Ex 5), why do features get equal credit despite unequal marginal gains? ::: Symmetry: averaging feature 1's gain over orderings ( and ) yields the same as feature 2's, so symmetric players share equally. A LIME weight at equals what, and why exponential? ::: Exactly ; the exponential is smooth, always positive, and finite at (unlike ).