6.3.2Interpretability & Explainability

Feature attribution (SHAP, LIME)

2,816 words13 min readdifficulty · medium1 backlinks

Core concepts

Feature attribution assigns a numerical importance score to each input feature for a single prediction. Unlike global feature importance (e.g., permutation importance), attribution is instance-specific: the same feature can be critical for one prediction and irrelevant for another.

The challenge: modern models (deep nets, ensembles) are non-linear, interactive compositions. A feature's contribution depends on other features' values. We need a principled way to decompose f(x)f(x) into additive contributions.

Figure — Feature attribution (SHAP, LIME)

LIME: Local Interpretable Model-agnostic Explanations

The core idea

LIME treats the model as a black box and builds a locally faithful linear approximation.

WHY linear? Humans interpret linear models easily: "feature xix_i increases prediction by wiw_i per unit." Even if ff is globally non-linear, near xx it might be roughly linear.

HOW it works:

  1. Generate perturbations: Sample new instances zZz \in \mathcal{Z} near xx by randomly togling/perturbing features.
  2. Weigh by proximity: Assign weight πx(z)=exp(d(x,z)2σ2)\pi_x(z) = \exp\left(-\frac{d(x, z)^2}{\sigma^2}\right) so close neighbors matter more.
  3. Fit local linear model: Solve
ξ(x)=argmingGL(f,g,πx)+Ω(g)\xi(x) = \arg\min_{g \in G} \mathcal{L}(f, g, \pi_x) + \Omega(g)

where GG is the class of linear models, L\mathcal{L} is weighted squared loss, Ω(g)\Omega(g) penalizes complexity (e.g., L1 to select few features).

The coefficients of gg are the LIME explanations: g(z)=w0+i=1dwizig(z) = w_0 + \sum_{i=1}^d w_i z_i.


SHAP: SHapley Additive exPlanations

Game-theoretic foundation

SHAP is grounded in Shapley values from cooperative game theory. Imagine features are players in a coalition, and the "payout" is the model's prediction.

The question: How much does each player (feature) contribute to the difference between f(x)f(x) and the average prediction f()f(\emptyset)?

DERIVATION from scratch:

  1. Setup: We have dd features. For a subset SS, define fS(xS)f_S(x_S) as the expected prediction when we observe features in SS and marginalize out features not in SS:
fS(xS)=EXS[f(xS,XS)]f_S(x_S) = \mathbb{E}_{X_{-S}}[f(x_S, X_{-S})]

WHY marginalize? We replace missing features with their typical distribution, not zeros.

  1. Marginal contribution: When we add feature ii to coalition SS, the gain is: Δi(S)=fS{i}(xS{i})fS(xS)\Delta_i(S) = f_{S \cup \{i\}}(x_{S \cup \{i\}}) - f_S(x_S)

  2. Average over all orderings: There are d!d! orderings of features. For each ordering, ii arrives after some set SS. The number of orderings where ii arrives after exactly the features in SS is S!(dS1)!|S|! \cdot (d - |S| - 1)!. So the probability of that configuration is:

S!(dS1)!d!\frac{|S|! (d - |S| - 1)!}{d!}

Weighted average of Δi(S)\Delta_i(S) over all S∌iS \not\ni i gives the Shapley value.

Result: ϕi\phi_i is the fair credit to feature ii for moving the prediction from baseline f()f(\emptyset) to f(x)f(x).

Computing SHAP: the challenge

Exact Shapley values require 2d2^d evaluations (all subsets). For high-dimensional data, this is intractable.

Solutions:

  1. KernelSHAP (model-agnostic): Approximates Shapley values by fitting a weighted linear model (like LIME, but with specific Shapley weights). Converges to true Shapley as samples →∞.

  2. TreeSHAP (for tree models): Exploits tree structure to compute exact Shapley values in polynomial time. HOW: Traverse the tree, track feature splits, and compute expectations over paths.

  3. DepSHAP (for neural nets): Approximates using backpropagation + reference values.


LIME vs SHAP: the tradeoffs

| Aspect | LIME | SHAP | |--------|------| | Foundation | Locally faithful linear model | Shapley values (game theory) | | Guarantees | None (approximation-dependent) | Efficiency, symmetry, dummy axioms | | Compute cost | O(nsamples)O(n_{\text{samples}}), flexible | O(2d)O(2^d) exact; TreeSHAP O(TLD2)O(TLD^2), KernelSHAP O(n2d)O(n \cdot 2^d) approx | | Stability | High variance with few samples | More stable (principled) | | Additivity | No guarantee wif(x)base\sum w_i \approx f(x) - \text{base} | Exact: ϕi=f(x)ϕ0\sum \phi_i = f(x) - \phi_0 | | Use case | Quick, interpretable, when consistency isn't critical | When you need theoretically sound, consistent explanations |

WHY use LIME? Speed + simplicity. You can explain a one-off prediction in seconds with reasonable samples. Good for protyping, user-facing apps where "roughly right" is enough.

WHY use SHAP? When decisions are high-stakes (healthcare, finance) and you need defensible, consistent explanations. SHAP's additivity means you can decompose a prediction exactly. TreeSHAP is also fast for tree models.

Recall Explain to a12-year-old

Imagine your teacher gives you a grade on a group project. You want to know: "How much did I contribute vs my teammates?"

LIME is like asking few classmates nearby: "If I weren't in the group, would the grade be lower?" Then you average their answers. It's quick, but if you ask different people, you might get different answers.

SHAP is like the teacher carefully calculating: "If I imagine all possible groups with and without you, and average how much the grade changes when you're added, that's your exact contribution." It's fair, everyone agrees on it, but it takes longer to compute.

Both help you understand why the grade (prediction) is what it is, one feature (person) at a time.


Connections 6.301-Global-vs-local-interpretability – Feature attribution is the canonical local method.

  • 6.2.03-Attention-mechanisms – Attention weights are a form of feature attribution (though not Shapley-based).
  • 5.4.02-Permutation-importance – Global feature importance; SHAP can be aggregated to recover similar rankings.
  • 6.3.03-Counterfactual-explanations – Alternative local explanation: "change xix_i to xix_i' to flip the prediction."
  • 3.5.04-Tree-ensembleinterpretation – TreeSHAP leverages tree structure for fast exact computation.

#flashcards/ai-ml

What is feature attribution? :: Assigning a numerical importance score to each input feature for a single prediction, showing how much each feature contributed to that specific output.

What makes LIME "local"?
LIME fits a simple interpretable model (linear) in a small neighborhood around the instance xx, weighted by proximity πx(z)=exp(d(x,z)2/σ2)\pi_x(z) = \exp(-d(x,z)^2/\sigma^2).

Derive the LIME objective function :: Start with weighted squared loss L=zπx(z)[f(z)g(z)]2\mathcal{L} = \sum_z \pi_x(z)[f(z) - g(z)]^2 (closeness to ff near xx), add complexity penalty Ω(g)=λw1\Omega(g) = \lambda \|w\|_1 (sparsity), minimize their sum to get locally faithful sparse linear gg.

What are Shapley values?
The unique fair attribution satisfying efficiency (ϕi=f(x)f()\sum \phi_i = f(x) - f(\emptyset)), symmetry, dummy, and linearity. For feature ii: ϕi=SS!(dS1)!d![fS{i}fS]\phi_i = \sum_S \frac{|S|!(d-|S|-1)!}{d!}[f_{S \cup \{i\}} - f_S], the marginal contribution averaged over all coalitions.
Why do SHAP values sum to the prediction?
By the efficiency axiom of Shapley values: f(x)=ϕ0+i=1dϕi(x)f(x) = \phi_0 + \sum_{i=1}^d \phi_i(x), where ϕ0\phi_0 is the base rate. This is exact, not approximate.
How does TreeSHAP achieve polynomial time?
Exploits tree structure: for each split, compute the fraction of training data going each direction, use that to weight the alternative branch's expectation. No need to enumerate all 2d2^d subsets explicitly.
LIME high variance mistake and fix
Using too few perturbations (e.g., 10) makes the linear fit unstable; explanations vary wildly between runs. Fix: Use 1,000–10,000 samples and check stability across multiple runs.

Key difference: LIME vs SHAP guarantees :: LIME is an approximation with no theoretical guarantees on fidelity or additivity. SHAP has axiom-based guarantees: efficiency, symmetry, dummy player, making it theoretically sound and consistent.

When to use LIME over SHAP?
When you need speed and simplicity for protyping or low-stakes user-facing explanations, and approximate local fidelity is sufficient. LIME can explain any model type with flexible sampling.
What is fS(xS)f_S(x_S) in Shapley derivation?
The expected prediction when features in subset SS are observed at xSx_S and features not in SS are marginalized: fS(xS)=EXS[f(xS,XS)]f_S(x_S) = \mathbb{E}_{X_{-S}}[f(x_S, X_{-S})]. This defines the baseline for computing marginal contributions.

Concept Map

assigns

contrasts with

relies on

approximates f near x

step 1

step 2

step 3 fit

penalized by

yields

are the

also computed by

Feature attribution

Local explanation

Global importance

Instance-specific score

LIME

Generate perturbations

Weigh by proximity

Local linear model

LIME coefficients

L1 sparsity penalty

SHAP

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Feature attribution ka matlab hai ki jab model koi ek prediction deta hai—jaise "is loan application ko reject karo"—to hum ye samajhna chahte hain ki konsi feature ne kitna contribute kiya. Global importance sirf overall batati hai (credit score important hai), lekin local attribution kehti hai "is specific case mein, Alice ki520 credit score ne -0.3 ka negative push diya."

LIME ka approach simple hai: model ko black-box maan lo, instance x ke pas kuch fake samples banao (perturbations), unpe model run karo, aur ek linear approximation fit kar lo. Jo weights milenge wo batayenge ki local region mein kaun si feature kitna matter karti hai. Fast hai, lekin thoda unstable bhi—kam samples pe explanation change ho sakta hai.

SHAPzyada mathematical hai, game theory se ata hai (Shapley values). Ye guarantee deta hai ki sabhi features ka contribution exactly add up hoga prediction tak. TreeSHAP tree models ke liye bahut fast hai. SHAP ka fayda: consistent aur fair explanations. Agar high-stakes decision hai (medical, financial), to SHAP better choice hai kyunki theoretically sound hai. Dono methods AI ko transparent banate hain—"black box" ko "glass box" mein convert karte hain, jo trust aur debugging ke liye zaroori hai.

Go deeper — visual, from zero

Test yourself — Interpretability & Explainability

Connections