Explainable AI (SHAP, LIME)

Explainable AI (SHAP, LIME)

How explanation methods such as SHAP and LIME reveal which inputs drive a machine-learning model's predictions, with guidance on their assumptions, pitfalls and responsible use.

What it is

Explainable AI (XAI) is a collection of methods for understanding why a machine-learning model produced a particular prediction, or how it behaves overall. Many accurate models, such as gradient-boosted trees, random forests and deep networks, are “black boxes”: they perform well but their internal logic is hard to read. Explanation methods help scientists check whether a model uses sensible features, help clinicians and engineers trust or challenge a result, help debug errors and bias, and sometimes generate scientific hypotheses. Two of the most widely used tools are LIME (Local Interpretable Model-agnostic Explanations, Ribeiro et al., 2016) and SHAP (SHapley Additive exPlanations, Lundberg and Lee, 2017). Both are post-hoc and model-agnostic: they are applied to a trained model and treat it as a function.

It answers questions such as: Which variables pushed this patient’s risk score up or down? Which features matter most across the whole data set? Is the model relying on a spurious cue, such as a hospital tag or an image watermark? How would the prediction change if a feature changed? Explanations describe the model’s behavior, not necessarily the real-world cause, which is a critical distinction.

Fundamentals

Global and local explanations

Global explanations describe the model overall: feature importance rankings, partial dependence plots (average prediction as one feature varies), and accumulated local effects (ALE, more reliable with correlated features). Local explanations describe one prediction: why this sample got this output. Intrinsically interpretable models (linear models, short decision trees, generalized additive models, rule lists) are transparent by design, and for high-stakes decisions some researchers (Cynthia Rudin) argue they should be preferred over explaining a black box when they perform comparably. Permutation importance measures how much the model’s score drops when one feature’s values are shuffled, a simple, model-agnostic global measure (best computed on held-out data; see decision trees and random forests).

LIME

LIME explains a single prediction by fitting a simple, interpretable model locally. It perturbs the instance (turning words on and off in text, superpixels in an image, or sampling around a tabular point), gets the black-box predictions for the perturbed samples, weights samples by their proximity to the original instance, and fits a sparse weighted linear model to them. The coefficients show which features pushed the prediction up or down in that neighborhood. LIME is fast, flexible and intuitive, but results depend on choices such as the kernel width and the perturbation scheme, can vary between runs (instability), and the local linear model may not be faithful to the black box.

SHAP and Shapley values

SHAP assigns each feature a contribution based on Shapley values from cooperative game theory. The Shapley value of feature i averages its marginal contribution over all subsets S of the other features: φi = ΣS⊆F∖{i} [|S|!(|F| − |S| − 1)!/|F|!] (f(S ∪ {i}) − f(S)), where f(S) is the model’s expected output when only the features in S are known (the others are averaged over background data). The explanation is additive: f(x) = φ0 + Σφi, where φ0 is the average prediction, so contributions sum exactly to the difference between this prediction and the baseline. Shapley values are the unique attribution satisfying fairness axioms (efficiency, symmetry, dummy, additivity). Exact computation needs 2|F| evaluations, so approximations are used: TreeSHAP (exact and fast for tree ensembles), KernelSHAP (model-agnostic, sampling-based, slow), DeepSHAP and GradientSHAP for neural networks, and LinearSHAP. Outputs: force and waterfall plots for single predictions, beeswarm summary plots showing global importance with direction of effect, dependence plots for a feature’s effect and interactions, and mean |φi| as a global importance ranking.

Other methods and image/text explanations

For deep networks on images, saliency maps, Grad-CAM (heat maps from gradients of the class score with respect to convolutional feature maps), Integrated Gradients (accumulating gradients along a path from a baseline to the input, satisfying completeness) and occlusion tests highlight influential pixels (see CNNs). For transformers, attention maps are sometimes shown, but attention weights are not reliable explanations by themselves, and gradient-based or perturbation-based attributions are preferred. Counterfactual explanations state what minimal change in inputs would flip the decision (“if income were 5,000 higher, the loan would be approved”), useful for recourse.

Pitfalls and good evidence

Correlated features make attributions ambiguous: with the usual “marginal” approach, SHAP can evaluate unrealistic feature combinations, and importance is split among correlated variables. Explanations can be unfaithful (not reflecting what the model really computes), unstable (changing with small perturbations), or even manipulated adversarially to hide bias. Most importantly, explanation is not causation: a high SHAP value means the model uses the feature, not that changing it in the real world would change the outcome. Sanity checks, such as randomizing model weights and verifying that explanations change (Adebayo et al., 2018), and comparing methods, increase confidence. Explanations should be tested with domain experts and, for decision support, with user studies.

Software, typical applications and limits

Applications: understanding risk models in medicine and finance, validating scientific models (does a property predictor use chemically sensible substructures?), debugging data leakage, and communicating results. Limits: computational cost for large models, dependence on background data, ambiguity with correlated features and the gap between explanation and mechanism.

Basic operation

  1. Train and validate the model. Make sure it performs well before explaining it.
  2. Define the question. Decide whether global understanding or a single case is needed.
  3. Choose the method. Use TreeSHAP for trees, Kernel or Deep SHAP otherwise, and LIME for quick local views.
  4. Choose background data. Use a representative reference sample from the training set.
  5. Compute attributions. Run the explainer on held-out samples.
  6. Visualize. Create summary, dependence and waterfall plots.
  7. Check robustness. Compare methods and test stability, and review with a domain expert.
  8. Collect the output. You obtain feature attributions per sample and overall; background selection and correlated features are covered in the video.

Good practice

  • Do not equate importance with cause. Attribution describes the model, not the world.
  • Handle correlated features. Group them or use conditional methods.
  • Compare methods. Check that explanations agree.
  • Use held-out data. Explaining only training data can mislead.
  • Involve domain experts. They can spot nonsensical patterns.
  • Prefer interpretable models for high-stakes decisions. When outcomes affect health, liberty or livelihood, use transparent models where possible, document limitations, and do not present explanations as proof of fairness.

Key terms

  • Black-box model — a model whose internal reasoning is not directly interpretable.
  • Shapley value — a feature’s average marginal contribution across all feature combinations.
  • LIME — a method that explains one prediction with a local simple model.
  • Local / global explanation — explaining one prediction versus overall behavior.
  • Partial dependence — the average prediction as a function of one feature.
  • Counterfactual explanation — a minimal change to the input that would alter the outcome.
  • Grad-CAM — a heat-map method showing image regions that drive a network’s decision.

Video coming soon

About five minutes on Explainable AI (SHAP, LIME): how it works in detail, how the data is acquired, and how it is analyzed.

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping