Part XXI — Explainable AI & Model Interpretability · Chapter 5

LIME

Hook

Saliency maps and Grad-CAM — short for Gradient-weighted Class Activation Mapping — both needed to reach inside a convolutional network's own structure. What explains a prediction from a model with no gradients to inspect at all — a random forest, a rules engine, anything you can only query, never open up?

Intuition
local slope at x0=3.0: 6.00

The curve is a "black box" — LIME — short for Local Interpretable Model-agnostic Explanations — never sees its formula, only its outputs. Drag the query point x0x_0: LIME samples a few points near it, weights the close ones more than the far ones, and fits the simplest possible model — a straight line — through just that weighted neighborhood.

Formalize

Around a query point x0x_0, take samples xix_i nearby, weight each by a kernel that favors closeness, and fit a weighted linear regression:

wi=exp ⁣((xix0)22σ2),y^=slopex+interceptw_i = \exp\!\left(-\frac{(x_i-x_0)^2}{2\sigma^2}\right), \qquad \hat y = \text{slope}\cdot x + \text{intercept}
  • x0x_0 — the query point: the specific input whose prediction is being explained.
  • xix_i — one of the sample points drawn near x0x_0.
  • wiw_i — the weight given to sample xix_i, which shrinks the farther xix_i is from x0x_0.
  • σ\sigma — the kernel width, controlling how quickly the weight falls off with distance.
  • y^\hat y — the local linear model's predicted output.
  • slope,intercept\text{slope}, \text{intercept} — the two parameters of the local straight-line fit.
  1. A weighted least-squares fit

    The slope and intercept minimize iwi(yiy^i)2\sum_i w_i(y_i - \hat y_i)^2 — an ordinary least-squares fit, just with each point's error scaled by how close it is to x0x_0.

  2. Model-agnostic by construction

    Nothing here needs the black box's internals: LIME only ever calls it as a function, exactly like a saliency map does, but a linear fit instead of a per-input gradient.

Play
LIME's local slope: 10.00 — true derivative 2×5.0: 10.00

Drag x0x_0 anywhere and compare LIME's local slope to the curve's actual derivative at that point, 2x02x_0 — a fact LIME itself never gets to see, since it only ever queries the black box, never its formula. Watch how closely a local, linear approximation can still track a genuinely curved function, as long as it's never asked to explain anywhere but right around x0x_0.

Worked example

The black box computes f(x)=x2f(x)=x^2 (unknown to LIME). At x0=3x_0=3, with kernel width σ=1.5\sigma=1.5, LIME samples x{1,2,3,4,5}x \in \{1,2,3,4,5\}, giving y{1,4,9,16,25}y \in \{1,4,9,16,25\}:

  1. Fit the weighted local line

    The Gaussian kernel weight for each offset dxdx, w=exp(dx2/4.5)w=\exp(-dx^2/4.5):

    • dx=2dx=-2: w0.411w\approx0.411
    • dx=1dx=-1: w0.801w\approx0.801
    • dx=0dx=0: w=1w=1
    • dx=1dx=1: w0.801w\approx0.801
    • dx=2dx=2: w0.411w\approx0.411

    These are symmetric around x0=3x_0=3, so the weighted mean of xx is exactly xˉ=3\bar x=3, and the weighted mean of yy comes out to yˉ10.43\bar y\approx10.43 (pulled above f(3)=9f(3)=9 by the two farthest, largest-yy samples). Plugging every sample's (xixˉ,yiyˉ)(x_i-\bar x, y_i-\bar y) pair into the weighted least-squares slope formula, wi(xixˉ)(yiyˉ)/wi(xixˉ)2\sum w_i(x_i-\bar x)(y_i-\bar y) \big/ \sum w_i(x_i-\bar x)^2, gives slope =6=6; the intercept then follows from yˉslopexˉ10.436(3)7.57\bar y - \text{slope}\cdot\bar x \approx 10.43-6(3) \approx-7.57.

  2. Compare to the true derivative

    f(x)=2xf'(x) = 2x, so f(3)=6f'(3)=6 — LIME's local slope matches the exact derivative, even though LIME never computed a derivative at all, only fit a line through five sampled points.

  3. But the line doesn't pass through (3, 9)

    At x=3x=3 the fitted line gives 6(3)7.5710.436(3)-7.57 \approx 10.43, not 99. LIME's local model tracks the slope faithfully — that's what makes it a good explanation of sensitivity — but it's a regression through a neighborhood, not an interpolation pinned to the exact point.

Checkpoint

Drag x₀ until LIME's local slope reaches at least 10.

local slope: 2.00
Drag the slider to try it
Summary
wi=exp ⁣((xix0)22σ2),y^=slopex+interceptw_i = \exp\!\left(-\frac{(x_i-x_0)^2}{2\sigma^2}\right), \qquad \hat y = \text{slope}\cdot x + \text{intercept}

LIME's entire trick is scope: don't explain the whole model, just the tiny neighborhood around one prediction, where almost anything looks roughly linear. That's also its limitation — the same black box gets a different local explanation at every point you ask about, and none of them say anything reliable once you're far from where they were fit. The next two chapters take a different approach: a single, game-theoretically justified way to split credit among features, valid everywhere at once.