Inferensys

Glossary

KernelSHAP

A model-agnostic implementation of Shapley additive explanations that uses a weighted linear regression on a sample of coalitions to estimate SHAP values for any black-box model.
ML engineer managing model versions on laptop, version history visible, technical Git-like workflow.
MODEL-AGNOSTIC SHAPLEY VALUE ESTIMATION

What is KernelSHAP?

KernelSHAP is a model-agnostic implementation of Shapley additive explanations that uses a weighted linear regression on a sampled set of feature coalitions to estimate SHAP values for any black-box model's predictions.

KernelSHAP is an algorithm that bridges game-theoretic fairness with practical machine learning interpretability by framing the estimation of Shapley values as a linear regression problem. It constructs an interpretable local surrogate model by sampling coalitions of features—treating absent features as marginalized over background data—and weighting each coalition's model prediction using a Shapley kernel derived directly from cooperative game theory axioms.

The method satisfies three critical properties: local accuracy (the explanation sums to the prediction difference from a baseline), missingness (absent features receive zero attribution), and consistency (a feature's attribution never decreases if its marginal contribution increases). Because KernelSHAP requires evaluating the model on exponentially many coalitions, practical implementations use a sampling approximation that maintains unbiasedness while trading computational cost for estimation variance.

MODEL-AGNOSTIC SHAPLEY VALUES

Key Characteristics of KernelSHAP

KernelSHAP is a theoretically grounded, model-agnostic method that estimates Shapley additive explanations by solving a weighted least squares problem over a sampled set of feature coalitions.

01

Additive Feature Attribution

KernelSHAP expresses a prediction as a linear sum of feature contributions plus a base value. This satisfies the completeness axiom, ensuring the sum of all SHAP values equals the difference between the model's output and the average prediction.

  • Base value: The expected model output over the background dataset
  • SHAP value: The marginal contribution of a feature, averaged over all possible coalition orderings
  • Additivity: Enables straightforward decomposition of any prediction into human-understandable components
02

Shapley Value Estimation via Sampling

Computing exact Shapley values requires evaluating all 2^M coalitions, which is computationally intractable. KernelSHAP approximates these values by sampling a subset of coalitions and solving a weighted linear regression.

  • Coalition vector: A binary mask indicating which features are present or absent
  • Weighting kernel: The Shapley kernel assigns higher weight to coalitions near the extremes (all features present or absent) and lower weight to mid-sized coalitions
  • Convergence: Accuracy improves with more samples, trading off between computational budget and explanation fidelity
03

Model-Agnostic Black-Box Interrogation

KernelSHAP treats the model as a black-box function and requires only the ability to query predictions for any coalition of features. This makes it applicable to any model type without access to gradients or internal weights.

  • Missing feature imputation: When a feature is absent from a coalition, its value is replaced by samples from a background dataset to marginalize over the feature's distribution
  • Framework compatibility: Works identically with scikit-learn, XGBoost, PyTorch, TensorFlow, and proprietary APIs
  • Trade-off: Model-agnosticism comes at the cost of higher computational overhead compared to model-specific methods like TreeSHAP
04

Local and Global Explanations

KernelSHAP generates local explanations for individual predictions, which can be aggregated to produce global feature importance insights across an entire dataset.

  • Local: Explains why a specific loan application was denied by attributing risk to income, credit history, and debt-to-income ratio
  • Global: Averaging absolute SHAP values across all predictions yields a ranked list of the most influential features in the model
  • Interaction effects: KernelSHAP can be extended to compute Shapley interaction values, capturing pairwise feature synergies that simple additive explanations miss
05

Shapley Kernel Weighting Function

The Shapley kernel is the mathematical weighting function that ensures the weighted linear regression recovers Shapley values from sampled coalitions. It is derived directly from Shapley value axioms.

  • Formula: π(z) = (M - 1) / (M choose |z|) * |z| * (M - |z|), where M is the total number of features and |z| is the coalition size
  • Property: Assigns infinite weight to the empty and full coalitions, enforcing exact agreement at the boundaries
  • Practical implementation: Infinite weights are replaced with large constants, and regularization is applied to maintain numerical stability during regression
06

Background Dataset Selection

The choice of background dataset critically impacts KernelSHAP explanations, as it defines the reference distribution used to simulate missing features and compute the base value.

  • Single reference: Using the dataset mean provides fast but potentially misleading explanations that ignore feature correlations
  • Representative sample: A random subset of training data (e.g., 100-1000 instances) better captures the feature distribution
  • K-medoids clustering: Selecting representative centroids reduces background size while preserving distributional coverage, improving computational efficiency without sacrificing accuracy
FEATURE ATTRIBUTION METHOD COMPARISON

KernelSHAP vs. TreeSHAP vs. LIME

A technical comparison of the three most widely deployed local explanation methods, contrasting their theoretical guarantees, computational cost, and model compatibility.

FeatureKernelSHAPTreeSHAPLIME

Model Compatibility

Model-agnostic (any black-box)

Tree ensembles only (XGBoost, LightGBM, Random Forest)

Model-agnostic (any black-box)

Theoretical Foundation

Shapley values (game theory)

Shapley values (game theory)

Local surrogate modeling (no game theory)

Completeness Axiom

Exact Computation

Sampling Strategy

Weighted linear regression on coalitions

Polynomial-time tree traversal

Perturbed samples around instance

Computational Cost

High (exponential in features)

Low (O(TLD^2) for trees)

Medium (user-defined samples)

Explanation Type

Additive feature attribution

Additive feature attribution

Surrogate model coefficients

Stability Across Runs

Low (stochastic sampling)

High (deterministic)

Low (stochastic sampling)

KERNELSHAP EXPLAINED

Frequently Asked Questions

Clear, technically precise answers to the most common questions about the KernelSHAP algorithm, its relationship to game theory, and its practical application in model debugging and audit.

KernelSHAP is a model-agnostic implementation of Shapley additive explanations that estimates SHAP values by solving a weighted linear regression over a sample of feature coalitions. It works by treating each feature as a player in a cooperative game where the payout is the model's prediction. To avoid the exponential complexity of evaluating all 2^M possible coalitions, KernelSHAP draws a random sample of coalitions (binary vectors indicating feature presence or absence). Each coalition is evaluated by integrating out missing features using a background dataset. The algorithm then fits a linear explanation model, weighting each coalition sample using a Shapley kernel weighting function that assigns higher importance to coalitions near the extremes—where all features are present or absent—and lower weight to coalitions of intermediate size. The resulting coefficients of the linear model are the SHAP values, which satisfy the additive property: the sum of all feature attributions plus a base value equals the model's output for that instance.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.