KernelSHAP is a model-agnostic algorithm that estimates Shapley values by solving a weighted linear regression problem. It treats each feature as a binary player (present or missing) and samples coalitions of features, evaluating the model's output for each coalition. A specially designed Shapley kernel weights these coalition samples so that the resulting linear model's coefficients exactly recover the Shapley values, satisfying the efficiency, missingness, and consistency axioms.
Glossary
KernelSHAP

What is KernelSHAP?
KernelSHAP is a model-agnostic implementation of SHAP that uses a specially weighted linear regression to approximate Shapley values efficiently for any machine learning model.
The algorithm approximates the intractable sum over all feature subsets by sampling coalitions and using a background dataset to impute missing features. KernelSHAP's kernel function assigns higher weight to coalitions with few or many features, reflecting their greater influence on Shapley value computation. While model-agnostic and theoretically grounded, KernelSHAP is computationally expensive for high-dimensional data, motivating model-specific alternatives like TreeSHAP and DeepSHAP for production deployments.
Key Characteristics of KernelSHAP
KernelSHAP is a model-agnostic implementation of SHAP that uses a specially weighted local linear regression to estimate Shapley values efficiently for any black-box model.
Shapley-Weighted Kernel Regression
KernelSHAP solves a weighted least squares problem to recover Shapley values. The Shapley kernel assigns infinite weight to the empty and full coalitions, and weights intermediate coalition sizes by:
π(z) = (M-1) / (M choose |z| * |z| * (M - |z|))
This weighting scheme guarantees the solution satisfies the efficiency, symmetry, dummy, and additivity axioms of Shapley values. The regression targets are the model's predictions on coalitionally masked inputs.
Coalition Vector Representation
Each coalition is encoded as a binary vector z' ∈ {0,1}^M, where 1 indicates a feature is present and 0 indicates it is absent. The explanation model is linear in this simplified space:
g(z') = φ₀ + Σ φᵢ z'ᵢ
To map back to the original feature space, a mapping function h_x(z') imputes missing features by sampling from the background dataset. This binary encoding is what makes the additive feature attribution framework computationally tractable.
Background Dataset Integration
KernelSHAP requires a background dataset to compute the expected model output when features are missing. When a feature is masked (set to 0 in the coalition vector), its value is replaced by samples drawn from this background:
- Single reference: Use the dataset mean or median for fast, approximate explanations
- Full distribution: Use k-means summarized samples for more accurate conditional expectations
- Interventional approach: Sample from the marginal distribution to break feature correlations, yielding causal SHAP interpretations
The choice of background directly impacts whether you get observational or interventional SHAP values.
Sampling-Based Approximation
Exact Shapley value computation requires evaluating all 2^M coalitions, which is infeasible for high-dimensional data. KernelSHAP approximates the solution by:
- Subsampling coalitions: Drawing
Ncoalitions according to the Shapley kernel distribution - Convergence guarantees: Error decreases at rate
O(1/√N)with the number of samples - Variance reduction: Techniques like paired sampling (evaluating both
z'and its complement) reduce estimator variance - nsamples parameter: Controls the trade-off between explanation accuracy and computational cost
LIME with a Theoretical Foundation
KernelSHAP can be understood as LIME with a specific kernel and weighting scheme. While LIME uses heuristically chosen proximity measures, KernelSHAP's kernel is derived from coalitional game theory to satisfy Shapley axioms:
- LIME's local surrogate model + Shapley kernel = Shapley value recovery
- The regression loss function minimizes
Σ π(z') [f(h_x(z')) - g(z')]² - This unification means KernelSHAP inherits LIME's model-agnostic flexibility while providing theoretically guaranteed fairness in feature attribution
Computational Complexity and Practical Limits
KernelSHAP scales exponentially with the number of features in the worst case. Practical considerations include:
- Feature limit: Typically feasible for
M < 30features without excessive sampling - Model evaluations: Each coalition requires a full model forward pass, making it expensive for large models
- Alternatives: For tree models, use TreeSHAP (exact, polynomial time); for deep networks, use DeepSHAP or GradientExplainer
- Mitigation: Feature grouping or dimensionality reduction before applying KernelSHAP can extend its applicability to wider datasets
KernelSHAP vs. LIME vs. TreeSHAP
A technical comparison of three prominent local explanation methods across their theoretical foundations, computational properties, and practical deployment characteristics.
| Feature | KernelSHAP | LIME | TreeSHAP |
|---|---|---|---|
Theoretical Foundation | Shapley values from cooperative game theory | Local surrogate model fitting | Shapley values with tree-specific optimization |
Model Compatibility | Any model (model-agnostic) | Any model (model-agnostic) | Tree-based models only (XGBoost, LightGBM, random forests, decision trees) |
Guarantees | Shapley axioms: efficiency, symmetry, dummy, additivity | No formal guarantees; fidelity depends on surrogate fit | Exact Shapley values with efficiency and consistency guarantees |
Explanation Type | Additive feature attribution with Shapley values | Locally faithful linear surrogate model | Additive feature attribution with exact Shapley values |
Computational Complexity | O(2^M × L) where M = features, L = samples; exponential in feature count | O(N × L) where N = perturbed samples, L = surrogate training cost | O(TLD^2) where T = trees, L = leaves, D = depth; polynomial time |
Handling Feature Dependencies | Interventional SHAP breaks correlations; observational SHAP conditions on them | Perturbation sampling may create unrealistic instances if correlations ignored | Interventional approach by default; respects tree-splitting structure |
Sampling Strategy | Weighted linear regression with Shapley kernel weighting | Random perturbation around instance with proximity weighting | No sampling required; exact computation via tree traversal |
Convergence Properties | Converges to true Shapley values as samples approach 2^M | No convergence guarantee to any ground-truth quantity | Exact values computed in single pass; no convergence needed |
Frequently Asked Questions
Clear, technically precise answers to the most common questions about KernelSHAP, the model-agnostic implementation of Shapley additive explanations.
KernelSHAP is a model-agnostic implementation of the SHAP framework that uses a specially weighted linear regression to approximate Shapley values efficiently. It works by treating the explanation task as a cooperative game where each feature is a player. The algorithm samples feature coalitions by randomly masking subsets of input features, then evaluates the model's output for each coalition. A weighted linear regression model is fit to these coalition samples, where the weights are defined by the Shapley kernel: π(z) = (M-1) / (C(M,|z|) * |z| * (M-|z|)), where M is the total number of features and |z| is the coalition size. This kernel ensures the regression solution satisfies the Shapley axioms of efficiency, symmetry, dummy, and additivity. The resulting coefficients of the linear model are the approximate Shapley values for each feature. Because KernelSHAP only requires black-box access to model predictions, it can explain any model type—from gradient-boosted trees to deep neural networks—without needing internal gradients or architecture-specific optimizations.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Explore the foundational concepts, alternative implementations, and visualization tools that form the KernelSHAP ecosystem for model-agnostic explainability.
SHAP (SHapley Additive exPlanations)
The overarching game-theoretic framework that unifies additive feature attribution methods. KernelSHAP is one specific, model-agnostic implementation within this framework.
- Unifies LIME, DeepLIFT, and Shapley values under a single class of additive explanation models
- Guarantees three desirable properties: local accuracy, missingness, and consistency
- Provides the theoretical foundation that KernelSHAP's weighted linear regression approximates
Shapley Values
A solution concept from coalitional game theory that fairly distributes a total payout among players based on their marginal contributions. KernelSHAP directly estimates these values for ML features.
- Each feature is treated as a player in a cooperative game where the payout is the model prediction
- The Shapley value for a feature is the weighted average of its marginal contribution across all possible feature coalitions
- Computing exact Shapley values requires evaluating 2^M coalitions, making approximation essential for high-dimensional data
TreeSHAP
A model-specific alternative to KernelSHAP that computes exact Shapley values for tree-based models in polynomial time rather than exponential time.
- Exploits the internal structure of decision trees to track feature contributions along paths
- Computes exact values without sampling, eliminating approximation error
- Orders of magnitude faster than KernelSHAP for models like XGBoost, LightGBM, and random forests
- Not applicable to neural networks or other non-tree architectures where KernelSHAP remains necessary
Background Dataset
A representative sample of data used by KernelSHAP to compute the expected model output and to impute missing features during coalition evaluation.
- Replaces absent features with values drawn from the background to simulate their removal
- The choice of background dataset directly impacts SHAP value estimates
- Common strategies include using k-means centroids, a random sample, or the full training set
- Smaller background sets reduce computation time but may introduce sampling bias
SHAP Summary Plot
A visualization that combines global feature importance with feature effect directionality by displaying the distribution of SHAP values across all instances.
- Features ranked by mean absolute SHAP value, showing overall impact magnitude
- Each point is a single instance, colored by feature value (red = high, blue = low)
- Reveals whether high feature values push predictions upward or downward
- Exposes non-linear relationships and interaction effects at a glance
LIME (Local Interpretable Model-agnostic Explanations)
A predecessor and conceptual cousin to KernelSHAP that also uses local surrogate models to explain individual predictions. KernelSHAP's kernel was derived from LIME's framework.
- LIME samples instances around a prediction and fits a sparse linear model
- KernelSHAP improves on LIME by using a specially weighted kernel that guarantees Shapley value convergence
- LIME's kernel is heuristic; KernelSHAP's kernel is theoretically grounded in game theory
- Both are model-agnostic, but KernelSHAP provides stronger theoretical guarantees for feature attribution

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us