skip to content

How do lime's LimeTextExplainer and LimeImageExplainer define the features they switch off, and what replaces a switched-off feature?

level: middleimportance: nice to knowfreq 7%

answer

  1. words and regions, not columns
  2. bag of words removes every copy
  3. a mask token when order matters
  4. quickshift regions, mean-colour fill
  5. kernel widths 25 and 0.25

basics

~10 s

LimeTextExplainer's features are words: removed everywhere with bow=True, or masked by position with bow=False. LimeImageExplainer's features are superpixels from quickshift segmentation, filled with their own mean colour, or with hide_color when it is set.

solid answer

~40 s

`LimeTextExplainer` splits text with `split_expression=r'\W+'`. With `bow=True` a feature is a distinct word and switching it off removes every occurrence; with `bow=False` a feature is a position, replaced by `mask_string` (`'UNKWORDZ'` by default); `char_level=True` works on characters. Its `classifier_fn` receives a list of raw strings, so a TF-IDF plus logistic-regression model is passed as its pipeline's `predict_proba`; distances are cosine and `kernel_width=25`. `LimeImageExplainer` (`kernel_width=0.25`) segments the image with quickshift (`kernel_size=4, max_dist=200, ratio=0.2`) unless given a `segmentation_fn`. With `hide_color=None` a switched-off superpixel is filled with its own mean colour, otherwise with `hide_color`. Its `explain_instance` defaults to `top_labels=5`, `num_samples=1000` and `batch_size=10`, and `get_image_and_mask` turns the result into a mask.

go deeper

for a junior

Recall that text features are words and image features are superpixels, and that the text explainer's classifier_fn takes raw strings, so you pass the whole pipeline.

for a middle

Explain bag-of-words removal against positional masking with mask_string, and quickshift superpixels filled with their mean colour unless hide_color is set.

for a senior

Show judgment on the hiding choice and the segmentation: a fill colour or region size the model reacts to changes the explanation, so inspect both before trusting a mask.

for a principal

Weigh whether word- or region-level explanations answer what reviewers of text and image models are asking, and what each costs per item in model calls.

## The same recipe, different features lime's three explainers share one recipe: switch parts of the input off at random, ask the model about each variant, weight the variants by how close they stay to the original, and fit a sparse linear surrogate. What changes between them is **what a feature is** and **what "off" means**. For tabular data a feature is a column. For text and images lime uses an *interpretable representation*: a binary vector saying which words, or which image regions, are present. In lime 0.2.0.1 the text and image explainers make different choices for both. ## Text: words or positions `lime.lime_text.LimeTextExplainer` explains a classifier over raw strings, such as an airline's complaint classifier built from a TF-IDF vectoriser and a logistic regression. | Setting | Default | Effect | |---|---|---| | `split_expression` | `r'\W+'` | a regex, or a callable, that splits the text into tokens | | `bow` | `True` | a feature is a distinct word; switching it off removes every occurrence | | `mask_string` | `None`, meaning `'UNKWORDZ'` | with `bow=False`, what replaces a word at a position | | `char_level` | `False` | when `True`, characters are the features | | `kernel_width` | `25` | width of the kernel over cosine distance | Key points: - With `bow=True`, "refund" appearing three times is **one** feature. Use `bow=False` when the model uses word order (n-grams, sequence models): each position becomes its own feature, so a word can matter in one place and not another, and it is masked instead of deleted. - Each perturbed sample removes a random number of features, from one up to all of them; distances are cosine distances between the presence vectors, multiplied by 100. - `classifier_fn` receives a **list of strings** and must return an `(n_samples, n_classes)` probability array. Pass the whole pipeline's `predict_proba`, not the bare classifier that expects a TF-IDF matrix. - `explain_instance` defaults to `num_samples=5000` and `num_features=10`, and the text explainer has no regression mode. `exp.as_list(label=1)` then lists `(word, weight)` pairs. ## Images: superpixels `lime.lime_image.LimeImageExplainer` explains an image classifier, such as a model that flags baggage damage in photos. Pixels are too many and too small to be features, so lime groups them into **superpixels**, contiguous regions of similar colour, and switches whole regions on or off. | Setting | Default | Effect | |---|---|---| | `segmentation_fn` | `None`, meaning quickshift with `kernel_size=4, max_dist=200, ratio=0.2` | how the image is cut into superpixels | | `hide_color` | `None` | `None` fills each switched-off superpixel with its own mean colour; a value such as `0` fills it with that value | | `top_labels` | `5` | explains the five most probable classes, ignoring `labels` | | `num_features` | `100000` | effectively every superpixel | | `num_samples` | `1000` | perturbed images sent to the model | | `batch_size` | `10` | images per `classifier_fn` call | | `random_seed` | `None` | seed for the segmentation, drawn from the explainer's generator when unset | | `kernel_width` (constructor) | `0.25` | width of the kernel over cosine distance | Two details catch people. The constructor docstring says `kernel_width` defaults to `sqrt(number of columns) * 0.75`, a sentence copied from the tabular explainer; the default in the signature is `0.25`. And a 2-D greyscale array is converted to RGB before segmentation, so `classifier_fn` receives three-channel images. What "off" means shapes the explanation: a mean-colour fill keeps a region's average tone, while a black fill paints a dark patch the model may react to on its own. Gradient saliency and occlusion maps answer a related question by other means and are not part of this package. ## Reading the image result `explain_instance` returns an `ImageExplanation`. `get_image_and_mask(label, positive_only=True, negative_only=False, hide_rest=False, num_features=5, min_weight=0.)` returns an `(image, mask)` pair: 1. pick a label that was explained, one of `exp.top_labels`, or a `KeyError` follows; 2. keep `positive_only=True` for regions that support the class, set `negative_only=True` for regions against it, or set both to `False` for both; 3. draw the outline with scikit-image's `mark_boundaries(image, mask)`. ## Practical checks - Look at the segmentation before reading weights: regions a person would not recognise make a mask hard to interpret. - Fix `random_seed` when the same superpixels are needed across runs, and swap the segmentation through `lime.wrappers.scikit_image.SegmentationAlgorithm` when quickshift's regions are wrong for the images. - Budget the model calls: 1,000 forward passes per image by default, in batches of 10, and 5,000 classifier inputs per document for text.

  • When should a lime text explanation use bow=False?
    When the classifier depends on word order or position, such as n-grams or a sequence model, and with `char_level=True`, as the docstring advises. Each occurrence then becomes its own feature and is replaced by `mask_string` (`'UNKWORDZ'` by default for words) instead of being deleted, which keeps the text's length and word positions intact.
  • How do you make LimeImageExplainer cut the image differently?
    Pass a `segmentation_fn` built with `lime.wrappers.scikit_image.SegmentationAlgorithm`, which wraps scikit-image's `'quickshift'`, `'slic'` and `'felzenszwalb'`, for example `SegmentationAlgorithm('slic', n_segments=100, compactness=1, sigma=1)`. Larger regions mean fewer features and a cheaper fit; smaller regions give finer masks from the same 1,000 samples.

  • LimeTextExplainer passes a TF-IDF matrix to classifier_fn.
  • With bow=True, each occurrence of a word is a separate feature.
  • LimeImageExplainer switches individual pixels on and off.
  • hide_color=None blacks out the hidden regions.
  • LimeImageExplainer's kernel width defaults to sqrt(columns) * 0.75.