Quant Memo
Advanced

KernelSHAP

Fairly splitting credit for a prediction among every feature, the exact Shapley way, requires checking every possible subset of features — an exponential amount of work KernelSHAP avoids by sampling subsets and solving one cheap weighted regression instead.

Prerequisites: The Shapley Axioms for Feature Attribution, Global vs Local Explanations

Splitting credit for a group project's outcome fairly among teammates, in the game-theoretic sense, requires checking every possible sub-team — with or without each person — and averaging how much that person's presence changed the result across all of them. For a model with MM features, "every possible sub-team" means 2M2^M subsets. With even 20 features that is over a million subsets per prediction explained — computationally impossible to do exactly for most real models. KernelSHAP gets a very good approximation to those exact Shapley values without ever enumerating all of them.

The analogy: crediting teammates without checking every sub-team

Imagine estimating each teammate's contribution to a project's success not by exhaustively trying every possible sub-team combination, but by sampling a representative handful of combinations — some with almost everyone present, some with almost no one, most somewhere in between — and then solving for the fairest assignment of credit consistent with the outcomes you actually observed. KernelSHAP does exactly this: it samples "coalitions" (subsets of features either present or replaced by a baseline), observes what the model predicts for each, and fits a simple weighted linear regression whose coefficients converge to the exact Shapley values as more coalitions are sampled.

The mechanics

For a coalition zz (a binary mask of which of the MM features are "present," the rest replaced by baseline values), KernelSHAP evaluates the model's prediction f(z)f(z) and weights that observation using the Shapley kernel:

π(z)=M1(Mz)z(Mz)\pi(z) = \frac{M-1}{\binom{M}{|z|}\,|z|\,(M-|z|)}

where z|z| is the number of present features in that coalition. In words: this weighting formula deliberately upweights coalitions that are almost empty or almost full (which are most informative about a single feature's marginal effect) and downweights coalitions near half-full (which are numerous but less informative per-sample). Fitting a weighted least-squares regression of the sampled f(z)f(z) values on the coalition masks, using these weights, produces coefficients that are the Shapley value approximations — one per feature.

Worked example 1: exact Shapley values for a simple linear model

For a genuinely linear model f(x)=2x1+3x2x3f(x) = 2x_1 + 3x_2 - x_3 with baseline x=(0,0,0)x=(0,0,0) and instance x=(1,1,1)x=(1,1,1), Shapley values have a known closed form for linear models: each feature's contribution is simply its coefficient times its own deviation from baseline, because there is no interaction to split up. That gives ϕ1=2(1)=2\phi_1 = 2(1)=2, ϕ2=3(1)=3\phi_2=3(1)=3, ϕ3=1(1)=1\phi_3=-1(1)=-1, summing to 4=f(1,1,1)f(0,0,0)=404 = f(1,1,1) - f(0,0,0) = 4-0. KernelSHAP, run on this same model with enough sampled coalitions, recovers these same three numbers — a useful sanity check, since the exact answer is known here without needing any sampling at all.

Worked example 2: the kernel weight in practice

For M=3M=3 features, consider a coalition with z=1|z|=1 feature present. (31)=3\binom{3}{1}=3, so π(z)=23×1×2=0.333\pi(z) = \frac{2}{3\times1\times2}=0.333. For z=2|z|=2: (32)=3\binom{3}{2}=3, so π(z)=23×2×1=0.333\pi(z)=\frac{2}{3\times2\times1}=0.333 — same weight, by the kernel's built-in symmetry between "mostly absent" and "mostly present" coalitions. Compare a coalition with z|z| near M/2M/2 in a larger example with M=10M=10, z=5|z|=5: (105)=252\binom{10}{5}=252, giving π(z)=9252×5×5=0.0014\pi(z) = \frac{9}{252\times5\times5}=0.0014 — over 200 times smaller per-coalition weight than the extreme cases, exactly the "upweight the edges, downweight the middle" behavior the kernel formula is designed to produce.

coalition size |z| → high weight high weight low weight (mid-size coalitions)
The Shapley kernel π(z) upweights coalitions with almost no features present or almost all present — those are most informative about one feature's marginal effect.

Regression explorer
amber = residuals
fitted slope 1.133true slope 1.00 0.642SSres 42.0

The final step of KernelSHAP is literally this: a (weighted) regression fit, but on sampled coalition masks and their model outputs rather than raw features and outcomes.

KernelSHAP approximates the exact Shapley feature attributions — which would require evaluating a model on all 2M2^M feature subsets — by sampling a manageable number of coalitions, weighting them with the Shapley kernel, and solving one weighted linear regression whose coefficients converge to the true Shapley values.

What this means in practice

KernelSHAP works with any model as a black box — it never looks inside, only at input/output pairs — making it the standard choice for explaining ensembles, neural networks, or any model without a faster specialized SHAP variant like TreeSHAP, at the cost of needing many model evaluations per explained prediction and being sensitive to the choice of baseline used to represent "absent" features.

The common mistake is running KernelSHAP with too few sampled coalitions and treating the resulting attributions as exact. With few samples, the weighted regression is underdetermined and its coefficients carry substantial sampling variance — re-running the explanation on the same instance can give noticeably different attribution values. Checking convergence (attributions stabilizing as sample count increases) matters as much as running the method at all.

Related concepts

Practice in interviews

Further reading

  • Lundberg & Lee, A Unified Approach to Interpreting Model Predictions (2017)
  • Shapley, A Value for n-Person Games (1953)
ShareTwitterLinkedIn