KernelSHAP (also written Kernel SHAP) is a model-agnostic method that estimates Shapley values for the features of a single prediction by fitting a weighted linear regression over sampled feature coalitions. In Explainable AI, it is the general-purpose estimator of the SHAP framework: it treats the model as a black box and needs only the ability to call its prediction function.
The method was introduced together with the SHAP framework in 2017. Its central result is that a specific weighting, the Shapley kernel, makes the solution of an ordinary weighted least squares problem equal to the exact Shapley values. KernelSHAP therefore connects the game-theoretic idea of fair credit allocation with the practical approach of local surrogate models known from LIME.
How KernelSHAP estimates Shapley values
KernelSHAP explains one prediction by asking how the model output changes when subsets of features are switched off. A coalition is a binary vector that marks which features keep their real values; the switched-off features are replaced with values drawn from a background dataset, and the model output is averaged over those replacements.
The algorithm then fits a linear model in which each coefficient is the attribution of one feature. The procedure runs in four steps:
- Sample coalitions of features, favoring very small and very large coalitions.
- Evaluate the model on each coalition by filling missing features from the background data.
- Weight each coalition with the Shapley kernel, which depends only on coalition size.
- Solve a weighted least squares problem with the constraint that attributions sum to the prediction minus the base value.
The Shapley kernel assigns the highest weight to coalitions with very few or almost all features, because those comparisons reveal the most about individual contributions. The result satisfies local accuracy: the base value plus all attributions equals the model output for the explained instance.
Key parameters and their effects
KernelSHAP has few settings, but each one affects accuracy, runtime and meaning of the explanation. The table summarizes the parameters that practitioners tune most often.
| Parameter | What it controls | Practical guidance |
|---|---|---|
| Number of samples | How many coalitions are evaluated | Hundreds to a few thousand; more reduces variance |
| Background dataset | Reference values for missing features | Often 50 to 200 rows or k-means summaries |
| Link function | Scale on which attributions are additive | Identity for raw output, logit for probabilities |
| Regularization | Feature selection for many features | L1 or top-k selection can stabilize results |
| Number of features | Size of the coalition space | Exact enumeration feasible only below about 15 |
The background dataset defines the question the explanation answers. Attributions relative to an average customer differ from attributions relative to a set of rejected applicants, so the reference population must be chosen and reported deliberately.
KernelSHAP compared with other attribution methods
KernelSHAP trades speed for generality: it works on any model, but model-specific estimators are faster and often exact. The comparison below places it among neighboring feature attribution methods.
| Method | Model access | Exactness | Typical cost |
|---|---|---|---|
| KernelSHAP | Black box, predictions only | Approximate, converges with samples | High, many model calls |
| TreeSHAP | Tree ensemble internals | Exact for trees | Low, polynomial time |
| DeepSHAP | Neural network gradients and layers | Approximate, based on DeepLIFT | Low to moderate |
| LIME | Black box, predictions only | No Shapley guarantees | Moderate |
| Permutation feature importance | Black box, predictions and labels | Global, not per prediction | Moderate |
Compared with LIME, KernelSHAP uses a theoretically derived weighting instead of a heuristic distance kernel, so its attributions inherit the Shapley properties of efficiency, symmetry, dummy and additivity. Compared with TreeSHAP, it is the fallback when the model is not a tree ensemble or when only an API endpoint is available.
Limitations and common misinterpretations
KernelSHAP has well-known weaknesses that experienced users account for. The most important one is the feature independence assumption: filling missing features from marginal background data creates unrealistic combinations when features are correlated, such as a high income paired with a very young age, and the model is then queried outside its training distribution.
| Problem | Cause | Mitigation |
|---|---|---|
| Attributions change between runs | Too few sampled coalitions | Increase samples, fix the random seed |
| Credit split oddly between correlated features | Marginal sampling ignores dependence | Group features or use conditional methods |
| Explanations take minutes per instance | Many features times many model calls | Smaller background set, batch inference |
| Results read as causal effects | Attribution confused with intervention | State that attributions describe the model only |
A further caution concerns explanation faithfulness. A KernelSHAP value describes how the model uses a feature, not how the world works, and two equally accurate models can produce different attributions, a symptom of the Rashomon effect. Attributions should therefore support, not replace, domain review.
When to use KernelSHAP in practice
KernelSHAP is the right choice when no faster model-specific explainer exists and the number of explained predictions is moderate. The decision table summarizes typical situations.
| Situation | Recommended choice | Reason |
|---|---|---|
| Gradient-boosted trees or random forests | TreeSHAP | Exact and orders of magnitude faster |
| Model reachable only through an API | KernelSHAP | Needs predictions only |
| Ensemble mixing different model types | KernelSHAP | Treats the whole ensemble as one function |
| Large neural network on images or text | Gradient-based or partition explainers | Too many inputs for coalition sampling |
| Audit of a few high-stakes decisions | KernelSHAP with many samples | Runtime matters less than stability |
In libraries such as the SHAP package, InterpretML or Captum, KernelSHAP is typically run on a sample of predictions, and local attributions are then aggregated into global summaries such as mean absolute SHAP values.
Explained without jargon
Imagine a team wins a prize and wants to split it fairly. One way is to look at how the team does with and without each member. KernelSHAP does the same for a model’s prediction. The team members are the input features, and the prize is the prediction.
The method hides some features, checks the new prediction and repeats this many times. It then works out how much each feature pushed the result up or down. It works with any model. The cost is time, because it needs many model calls.
A typical use case
A regional bank uses a credit scoring model built as an ensemble of a neural network and a logistic regression. Regulators require the bank to give rejected applicants the main reasons for each decision. Because the ensemble has no tree structure, TreeSHAP cannot be used.
The data science team applies KernelSHAP to every rejected application, with a background set of 100 representative approved applicants and 2,000 sampled coalitions per explanation. Each explanation takes about four seconds. For one applicant with a score of 0.31 against a 0.50 threshold, the attributions show that a high debt-to-income ratio lowered the score by 0.12 and two recent missed payments lowered it by 0.08.
The bank turns the top three negative attributions into reason codes for the rejection letter. Quarterly reviews of aggregated attributions also reveal that a postal code feature carried unexpected weight, and the team removes it to reduce fairness risk.