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 features, "every possible sub-team" means 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 (a binary mask of which of the features are "present," the rest replaced by baseline values), KernelSHAP evaluates the model's prediction and weights that observation using the Shapley kernel:
where 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 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 with baseline and instance , 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 , , , summing to . 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 features, consider a coalition with feature present. , so . For : , so — same weight, by the kernel's built-in symmetry between "mostly absent" and "mostly present" coalitions. Compare a coalition with near in a larger example with , : , giving — 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.
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 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.
Practice in interviews
Further reading
- Lundberg & Lee, A Unified Approach to Interpreting Model Predictions (2017)
- Shapley, A Value for n-Person Games (1953)