Course contentsShow
← Machine Learning and Deep Learning
Lesson 3226 of 3,53870. Interpretability: Feature-Based MethodsFree lesson

LIME for Text Classification

Explaining text predictions by perturbing word presence and fitting local explanations.

LIME for Text Classification

You'll learn how LIME explains text classifier predictions by selectively hiding words and observing which ones matter most.

The Core Idea

When LIME interprets text predictions, it treats each document as a collection of words that can be turned "on" or "off." The algorithm creates perturbed versions of your input text by randomly removing words, feeds these modified texts to your classifier, and observes how predictions change. Words whose removal causes big prediction shifts are clearly important.

The process mirrors what you learned for tabular data, but the interpretable representation is binary: each word is either present (1) or absent (0). The perturbed samples are weighted by their similarity to the original text—versions with fewer word removals get more weight because they're "closer" to what you're actually explaining.

How It Works

  1. Perturb: Generate new text samples by randomly removing subsets of words from the original
  2. Predict: Run your black-box classifier on all perturbed versions
  3. Weight: Assign higher importance to perturbations closer to the original (fewer removals)
  4. Fit surrogate: Train a simple linear model on these perturbed samples in the interpretable word-presence space
  5. Extract coefficients: The linear weights reveal which words pushed the prediction toward or away from the predicted class

For example, if removing "excellent" from a movie review drops the positive sentiment score dramatically, that word gets a high positive coefficient in the local explanation.

Key Takeaway: LIME for text classification explains predictions by systematically removing words, observing prediction changes, and fitting a locally-weighted linear model that highlights which words mattered most for that specific prediction.