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) into additive contributions.
LIME treats the model as a black box and builds a locally faithful linear approximation.
WHY linear? Humans interpret linear models easily: "feature xi increases prediction by wi per unit." Even if f is globally non-linear, near x it might be roughly linear.
HOW it works:
Generate perturbations: Sample new instances z∈Z near x by randomly togling/perturbing features.
Weigh by proximity: Assign weight πx(z)=exp(−σ2d(x,z)2) so close neighbors matter more.
Fit local linear model: Solve
ξ(x)=argg∈GminL(f,g,πx)+Ω(g)
where G is the class of linear models, L is weighted squared loss, Ω(g) penalizes complexity (e.g., L1 to select few features).
The coefficients of g are the LIME explanations: g(z)=w0+∑i=1dwizi.
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) and the average prediction f(∅)?
DERIVATION from scratch:
Setup: We have d features. For a subset S, define fS(xS) as the expected prediction when we observe features in S and marginalize out features not in S:
fS(xS)=EX−S[f(xS,X−S)]
WHY marginalize? We replace missing features with their typical distribution, not zeros.
Marginal contribution: When we add feature i to coalition S, the gain is:
Δi(S)=fS∪{i}(xS∪{i})−fS(xS)
Average over all orderings: There are d! orderings of features. For each ordering, i arrives after some set S. The number of orderings where i arrives after exactly the features in S is ∣S∣!⋅(d−∣S∣−1)!. So the probability of that configuration is:
d!∣S∣!(d−∣S∣−1)!
Weighted average of Δi(S) over all S∋i gives the Shapley value.
Result:ϕi is the fair credit to feature i for moving the prediction from baseline f(∅) to f(x).
Exact Shapley values require 2d evaluations (all subsets). For high-dimensional data, this is intractable.
Solutions:
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 →∞.
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.
DepSHAP (for neural nets): Approximates using backpropagation + reference values.
| Aspect | LIME | SHAP |
|--------|------|
| Foundation | Locally faithful linear model | Shapley values (game theory) |
| Guarantees | None (approximation-dependent) | Efficiency, symmetry, dummy axioms |
| Compute cost | O(nsamples), flexible | O(2d) exact; TreeSHAP O(TLD2), KernelSHAP O(n⋅2d) approx |
| Stability | High variance with few samples | More stable (principled) |
| Additivity | No guarantee ∑wi≈f(x)−base | Exact: ∑ϕi=f(x)−ϕ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.
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 x, weighted by proximity πx(z)=exp(−d(x,z)2/σ2).
Derive the LIME objective function :: Start with weighted squared loss L=∑zπx(z)[f(z)−g(z)]2 (closeness to f near x), add complexity penalty Ω(g)=λ∥w∥1 (sparsity), minimize their sum to get locally faithful sparse linear g.
What are Shapley values?
The unique fair attribution satisfying efficiency (∑ϕi=f(x)−f(∅)), symmetry, dummy, and linearity. For feature i: ϕi=∑Sd!∣S∣!(d−∣S∣−1)![fS∪{i}−fS], 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), where ϕ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 2d 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) in Shapley derivation?
The expected prediction when features in subset S are observed at xS and features not in S are marginalized: fS(xS)=EX−S[f(xS,X−S)]. This defines the baseline for computing marginal contributions.
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.