Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Zoom in until the curve looks straight
Any smooth curve looks like a straight line if you zoom in far enough. LIME explains one prediction of a complicated model in the same way. It looks only at the model’s behaviour near that one input, and fits a straight-line model there. The line’s slopes say which features push this prediction up or down, and by how much.
When a model flags a part as defective or a flight segment as anomalous, an operator wants to know why this case was flagged. LIME gives a per-case answer for any model. Understanding how its answer depends on hidden settings protects you from trusting, or presenting, explanations that are artefacts of those settings.
- 1. LIME explains…
- 2. LIME's explanation comes from…
- 3. LIME works with…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.The algorithm by hand
Tabular LIME.
- Draw perturbed samples zₖ around the instance x, for example x + Gaussian noise. (The library samples from the training data’s feature distributions.)
- Get the black-box predictions f(zₖ).
- Weight each sample by the kernel πₓ(z) = exp(−‖z − x‖²/width²). A sample at x gets weight 1; at distance 1 with width 1, it gets e⁻¹ ≈ 0.37; at distance √2, e⁻² ≈ 0.14.
- Fit a weighted (ridge) linear regression of f(z) on z − x. Its slopes are the explanation.
Weighted slope, one feature. For y = z² at z = −1, 0, 1, 3, weighting only the points around 0 gives slope 0. Weighting the points at 0, 1 and 3 gives 22/7 ≈ 3.14. The weights decide which “local” region the line describes.
Effective sample size. (Σw)²/Σw² counts how many samples really inform the fit. Equal weights on 4 samples give 4; one dominant weight gives about 1.
Fidelity. The kernel-weighted R² of the surrogate against the model shows how well the line matches the model locally. A perfect fit scores 1; predicting the weighted mean scores 0.
- 1. Kernel weight at distance 2 with width 2?
- 2. The slopes of the weighted linear fit are…
- 3. Effective sample size measures…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.Does LIME find the local gradient?
For a smooth model, the ideal local linear explanation is the gradient ∇f(x). Exercise 1 uses a black box with a known gradient: f(x) = sin(2x₀) + x₁² + x₀x₂ (x₃ has no effect). The samples are x plus standard Gaussian noise.
Exercise 1 (kernel width 0.5, 2000 samples):
| instance x | true gradient | LIME slopes | weighted fidelity R² |
|---|---|---|---|
| (0, 0.5, 1, 0.3) | (3.00, 1.00, 0.00, 0.00) | (2.51, 1.07, −0.00, 0.03) | 0.951 |
| (0.8, −1, −0.5, 0) | (−0.56, −2.00, 0.80, 0.00) | (−0.60, −1.90, 0.90, −0.11) | 0.837 |
| (−0.6, 0.2, 0, 1) | (0.72, 0.40, −0.60, 0.00) | (0.48, 0.42, −0.67, 0.14) | 0.526 |
- With a reasonable width, LIME’s slopes were close to the gradient, though curvature biased them. At the third point it even ranked x₂ above x₀, the reverse of the true order.
- With a width of 0.05 at the first point, the effective sample size was 1.0 out of 2000 and every slope was 0.00. The kernel was so narrow, relative to the sampling spread, that no sample except one near x had any weight, and the ridge penalty shrank everything to zero. The explanation said “nothing matters” for a model with a gradient of 3.
from lime.lime_tabular import LimeTabularExplainer
exp = LimeTabularExplainer(X_train, mode="regression", kernel_width=0.75 * np.sqrt(X_train.shape[1]))
exp.explain_instance(x, model.predict, num_features=4, num_samples=5000).as_list()- 1. With width 0.05, LIME's slopes were…
- 2. The ideal local linear explanation of a smooth model is…
- 3. LIME's slopes differ from the gradient partly because of…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory and weaknesses
Objective. LIME solves ξ(x) = argmin over g in G of L(f, g, πₓ) + Ω(g), where:
- G is a class of interpretable models (sparse linear models, short trees);
- L is the πₓ-weighted squared loss on the samples;
- Ω penalises complexity (for example, at most K nonzero features, chosen by forward selection or a lasso path).
Text and images.
- Text: the perturbations remove words, and the features are word presence.
- Images: the perturbations switch superpixels off.
The interpretable representation is binary, so the explanation says which parts of the input mattered.
Known weaknesses.
- Kernel width. There is no principled choice. Results change with it (Garreau and von Luxburg, 2020, analysed tabular LIME in theory).
- Sampling. The library samples each feature independently from training statistics, which ignores correlations and creates unrealistic points, as PDPs do.
- Instability. Different random samples give different explanations, especially with few samples (Alvarez-Melis and Jaakkola, 2018).
- Manipulation. Slack et al. (2020) built models that behave in a biased way on real data but innocently on LIME’s off-distribution perturbations, so that LIME reports an innocent explanation.
Interview questions.
- How would you make LIME more trustworthy? Use many samples, check stability over seeds, report fidelity, tune the width on the scale of the data, and compare with SHAP or with gradients where available.
- LIME or SHAP? SHAP has axiomatic guarantees and additive consistency. LIME is faster and more flexible, but its explanations are less stable and depend on the kernel.
- 1. In LIME's objective, Ω(g) is…
- 2. For images, LIME's interpretable features are…
- 3. Slack et al. (2020) showed LIME can be…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.How kernel width and sample size shape the explanation
History.
- M. T. Ribeiro, S. Singh and C. Guestrin (2016) introduced LIME.
- D. Alvarez-Melis and T. Jaakkola (2018) showed that local explanations lack robustness.
- D. Garreau and U. von Luxburg (2020) analysed tabular LIME theoretically.
- D. Slack et al. (2020) fooled LIME and SHAP with adversarial models.
Exercise 2. The black box of exercise 1, at three points, with 10 seeds per setting. Columns: mean |error| of the seed-averaged slopes against the gradient; spread of the slopes across seeds; effective samples; how often LIME’s top feature differed from the true top feature.
| kernel width | samples | error of mean slopes | spread across seeds | effective samples | top feature wrong |
|---|---|---|---|---|---|
| 0.1 | 200 | 0.757 | 0.000 | 1.0 | 70% |
| 0.1 | 2000 | 0.740 | 0.072 | 1.4 | 67% |
| 0.3 | 200 | 0.247 | 0.309 | 2.7 | 20% |
| 0.3 | 2000 | 0.038 | 0.084 | 15.2 | 20% |
| 1.0 | 200 | 0.134 | 0.111 | 62.9 | 30% |
| 1.0 | 2000 | 0.117 | 0.026 | 614.7 | 33% |
| 3.0 | 200 | 0.210 | 0.114 | 186.8 | 33% |
| 3.0 | 2000 | 0.190 | 0.037 | 1873.6 | 33% |
- Too narrow: with width 0.1 almost no sample carried weight. The slopes collapsed to zero, perfectly reproducible (spread 0.000) and completely wrong. Low spread is not evidence of correctness.
- Too wide: with width 3, the fit described the whole sampled region, so the slope for the wavy sin(2x₀) term was averaged away, and more samples did not reduce the error.
- The sweet spot needed many samples. With width 0.3, the error fell from 0.247 to 0.038 when the samples went from 200 to 2000, and the spread across seeds fell from 0.31 to 0.08.
- Even at best, the top feature was wrong 20% of the time. At the third point, the true slopes 0.72 and −0.60 are close, and the explanation’s ranking flipped between runs.
LIME in AI, robotics and aerospace
- Operator support: a per-case explanation of why a classifier flagged a component, listing the top contributing measurements.
- Image inspection: superpixel LIME highlights the regions of a weld or composite image that drove a defect call.
- Validation: check explanations for stability across seeds and widths before they enter a report or a certification argument.
References
- M. T. Ribeiro, S. Singh, C. Guestrin (2016). “‘Why should I trust you?’: explaining the predictions of any classifier.” Proc. KDD.
- D. Alvarez-Melis, T. S. Jaakkola (2018). “On the robustness of interpretability methods.” ICML Workshop on Human Interpretability.
- D. Garreau, U. von Luxburg (2020). “Explaining the explainer: a first theoretical analysis of LIME.” Proc. AISTATS.
- D. Slack, S. Hilgard, E. Jia, S. Singh, H. Lakkaraju (2020). “Fooling LIME and SHAP: adversarial attacks on post hoc explanation methods.” Proc. AIES.