What Is Kernel SHAP?

Kernel SHAP

Kernel SHAP is a slow, perturbation-based Shapley approach that theoretically works for all types of models but is rarely used by teams in production at full scale. The method estimates SHAP values by sampling feature coalitions, querying the model on perturbed inputs, and fitting a weighted linear model whose coefficients approximate Shapley allocations. It is model-agnostic: the same code wraps tree ensembles, neural nets, or opaque scorers if you can call predict on edited rows.

When teams complain that SHAP is too slow, they often tested Kernel SHAP on wide tabular data or large images without switching to a model-specific explainer. Kernel SHAP is the fallback that always runs, not the default you want on every request.

Try Arize AX

Build better agents with Arize

Trace, evaluate, and learn. Build agents that work with Arize AX and start tracing your runs today.

Prefer open source? Try Arize Phoenix for self-hosted, open source agent observability.

Key takeaways

  • Kernel SHAP approximates Shapley values via coalition sampling and a weighted linear regression on model outputs.
  • It applies to any model with a predict function, which makes it flexible and expensive.
  • Runtime grows with feature count, sample count, and model inference cost per perturbation.
  • TreeSHAP and other specialized explainers are preferred for tree models when speed matters.
  • Use Kernel SHAP for small datasets, prototyping, or non-tree models without a dedicated SHAP implementation.

How Kernel SHAP works

Kernel SHAP follows a five-step pattern:

  1. Choose an instance to explain and a background dataset that defines the baseline.
  2. Sample feature coalitions (subsets of features present vs replaced with background values).
  3. Build perturbed inputs by merging coalition features from the instance with background values for absent features.
  4. Query the model on each perturbed row to get predictions.
  5. Fit a weighted linear model where features are indicators of presence in the coalition; coefficients approximate SHAP values for the instance.

The kernel weights emphasize coalitions of intermediate size, reflecting Shapley theory’s emphasis on marginal contributions as features enter in different orders.

The output is local to one row (or one batch of rows explained separately). Global stories require aggregating many local runs.

Why Kernel SHAP is slow

Cost is roughly: number of explained instances × number of coalition samples × cost of one model prediction × feature handling overhead.

Wide tables with hundreds of features need many samples for stable estimates. Deep models make each prediction costly. Explaining an entire validation set with Kernel SHAP can take hours or days without aggressive subsampling.

This explainability technique tends to be laborious in practice on anything but small data. It also tends to cause confusion among teams: when SHAP feels unusably slow, the first question is whether Kernel SHAP was the explainer.

Mitigations include explaining fewer rows, reducing background sample size, lowering coalition counts (accepting noisier values), or switching explainers when the model class allows.

Kernel SHAP vs TreeSHAP and other explainers

Explainer Model scope Speed (typical) Notes
Kernel SHAP Any predict callable Slow Default agnostic fallback
TreeSHAP Tree ensembles Fast Exact for trees
DeepSHAP Neural nets Moderate Approximate, architecture tied
Linear SHAP Linear models Fast Closed form for linear cases

If you train gradient boosted trees for fraud or churn, TreeSHAP is the practical choice for batch explanation. Reserve Kernel SHAP for architectures without a native SHAP path, or for auditing a small set of production errors.

When Kernel SHAP still makes sense

Reasonable use cases:

  • Prototyping on 50 features and 100 rows before investing in a faster explainer.
  • Custom scoring code where only a Python predict function exists and model internals are hidden.
  • Regulatory one-offs where you must document explanations for a bounded audit sample.
  • Non-tree baselines during model comparison when all candidates need the same explainer for fair local comparison.

Poor use cases:

  • Real-time explanation on every API response for wide features.
  • Full training-set global importance with default sample budgets.
  • High-frequency recompute on drifted streams without caching.

Operational tips

Fix a background set and seed. Kernel SHAP is stochastic when sampling coalitions; document sample counts and random seeds in model cards.

Handle missing values and categoricals consistently between training and perturbation. Replacing with background medians or modes changes explanations if preprocessing differs.

Watch correlated features. Kernel SHAP splits credit like any Shapley method; redundant columns yield unstable attributions across runs unless sample counts are high.

Pair local explanations with model, concept, and data drift checks. Attribution on a drifting feature confirms the model still leans on it; drift without attribution review misses scale effects across the population.

For governance, log which explainer produced values stored with each incident ticket. Kernel SHAP and TreeSHAP numbers are not interchangeable on the same row.

Kernel SHAP in the monitoring stack

Most teams do not stream Kernel SHAP into live dashboards. Instead, schedule offline jobs on stratified samples: recent false positives, high-score negatives, and random baselines. Feed results into AI model lifecycle management reviews before major releases.

When debugging classification near a threshold, Kernel SHAP on borderline cases clarifies which features pushed scores across the cutoff. Combine with guidance on shipping classification models with confidence so threshold policy and explanation samples align.

FAQ

Why is Kernel SHAP called “kernel” SHAP?

It uses a weighting kernel over feature coalition sizes when fitting the local linear model that approximates Shapley values. The kernel shape comes from the Shapley axioms, not from a radial basis function in input space.

Can Kernel SHAP explain text or image models?

In principle yes, if you define plausible perturbed inputs (mask tokens, hide patches) and a background distribution. In practice cost explodes with input dimension; specialized explainers or gradient methods are more common for deep nets.

How many samples do I need for stable Kernel SHAP values?

Depends on feature count and correlation structure. Start with library defaults on a small holdout, then increase samples until top feature rankings stabilize on repeated runs. There is no universal count; measure variance on your data.

Is Kernel SHAP more accurate than LIME?

Both approximate local behavior differently. Kernel SHAP targets Shapley-based allocations with specific axioms; LIME fits a local linear surrogate. Compare on known synthetic models when choosing for compliance narratives.

Should I use Kernel SHAP in production pipelines?

Only for bounded batch jobs or on-demand dispute review. For tree models, use TreeSHAP. For neural nets at scale, consider gradient attributions or sampling strategies designed for your architecture.

Don’t ship vibes.

Arize gives AI teams observability and evals to understand and improve agent performance.