KernelSHAP

Also known as: Kernel SHAP

On this page 7

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:

  1. Sample coalitions of features, favoring very small and very large coalitions.
  2. Evaluate the model on each coalition by filling missing features from the background data.
  3. Weight each coalition with the Shapley kernel, which depends only on coalition size.
  4. 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.

ParameterWhat it controlsPractical guidance
Number of samplesHow many coalitions are evaluatedHundreds to a few thousand; more reduces variance
Background datasetReference values for missing featuresOften 50 to 200 rows or k-means summaries
Link functionScale on which attributions are additiveIdentity for raw output, logit for probabilities
RegularizationFeature selection for many featuresL1 or top-k selection can stabilize results
Number of featuresSize of the coalition spaceExact enumeration feasible only below about 15
Main KernelSHAP parameters

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.

MethodModel accessExactnessTypical cost
KernelSHAPBlack box, predictions onlyApproximate, converges with samplesHigh, many model calls
TreeSHAPTree ensemble internalsExact for treesLow, polynomial time
DeepSHAPNeural network gradients and layersApproximate, based on DeepLIFTLow to moderate
LIMEBlack box, predictions onlyNo Shapley guaranteesModerate
Permutation feature importanceBlack box, predictions and labelsGlobal, not per predictionModerate
KernelSHAP and related explanation methods

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.

ProblemCauseMitigation
Attributions change between runsToo few sampled coalitionsIncrease samples, fix the random seed
Credit split oddly between correlated featuresMarginal sampling ignores dependenceGroup features or use conditional methods
Explanations take minutes per instanceMany features times many model callsSmaller background set, batch inference
Results read as causal effectsAttribution confused with interventionState that attributions describe the model only
Typical problems with KernelSHAP results

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.

SituationRecommended choiceReason
Gradient-boosted trees or random forestsTreeSHAPExact and orders of magnitude faster
Model reachable only through an APIKernelSHAPNeeds predictions only
Ensemble mixing different model typesKernelSHAPTreats the whole ensemble as one function
Large neural network on images or textGradient-based or partition explainersToo many inputs for coalition sampling
Audit of a few high-stakes decisionsKernelSHAP with many samplesRuntime matters less than stability
Choosing an estimator for SHAP values

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.