skip to content

In lime, what does LimeTabularExplainer.explain_instance return for one row of a classifier, and how do you read its as_list output?

level: juniorimportance: must knowfreq 38%

answer

  1. a stand-in's coefficients, not the model's
  2. probabilities in, not class predictions
  3. condition and weight pairs
  4. sorted by absolute weight
  5. label defaults to 1

basics

~20 s

It returns an Explanation object. as_list(label=1) gives (feature condition, weight) pairs sorted by absolute weight: coefficients of a local linear surrogate fitted to the classifier's probability for that label, so positive weights push that class up.

solid answer

~40 s

`explain_instance(data_row, predict_fn)` returns a `lime.explanation.Explanation`. For a classifier, `predict_fn` must return class probabilities, so you pass `predict_proba`; a function returning one value per row raises `NotImplementedError` in classification mode. `exp.as_list(label=1)` lists `(description, weight)` tuples, at most `num_features=10` of them, sorted by absolute weight. With the default discretisation a description reads like `lead_time > 120.00`, or `fare_class=basic` for a categorical column with names. Each weight is the surrogate's coefficient for "this row's feature is in that bin or category", on the scale of the predicted probability for that label: positive pushes it up. The `label` must be one the call explained (`labels=(1,)` by default, or the classes picked by `top_labels`, listed by `exp.available_labels()`), otherwise `as_list` raises a `KeyError`.

go deeper

for a junior

Recall that explain_instance needs predict_proba for a classifier and that as_list returns (condition, weight) pairs, sorted by absolute weight, for label 1 unless you ask for another.

for a middle

Explain that the weights are coefficients of a weighted linear surrogate fitted to perturbed copies of the row, and that binned conditions such as lead_time > 120.00 are what those coefficients attach to.

for a senior

Show you know the label trap: top_labels changes which classes exist in the explanation, and a probability array that does not sum to one only warns, so a batch job can store wrong-scale weights silently.

for a principal

Frame what an analyst may conclude from one as_list: a local, per-class statement about a fitted stand-in, which needs the prediction and the surrogate's fit quality shown beside it.

## What the call does `lime.lime_tabular.LimeTabularExplainer` explains one prediction at a time. You build the explainer once from the training data, then call `explain_instance(data_row, predict_fn)` for each row you want explained. For an airline's booking **no-show model** (a scikit-learn `HistGradientBoostingClassifier` on 26 booking features), `data_row` is one booking as a 1-D numpy array and `predict_fn` is the model's `predict_proba`. Inside the call, lime: 1. generates `num_samples` perturbed copies of the row (5,000 by default; the first is the row itself), 2. sends them all to `predict_fn` in a single call, 3. weights each copy by how close it stays to the original row, 4. fits a small weighted linear model, the **surrogate**, to the predicted probability of each label it was asked to explain, 5. returns a `lime.explanation.Explanation` holding the surrogate's coefficients. Why a weighted local linear model is a reasonable stand-in is a question about the local-surrogate idea, not about this package. What matters here is that every number you read back is a **coefficient of that surrogate**, not something read out of the gradient-boosted model. ## What predict_fn must return In the default `mode="classification"`, lime expects a 2-D array: one row per perturbed sample, one column per class. - Passing `model.predict` (one label per row) gives a 1-D array, and lime raises `NotImplementedError`, saying it does not support classifiers without probability scores. - If the rows do not sum to 1 (log-probabilities, raw margins), lime only **warns** and carries on, so the weights come out on the wrong scale without an error. - For a regressor, set `mode="regression"` on the explainer and pass `predict`. - `predict_fn` receives a numpy array, not the DataFrame the model may have been fitted on; wrap it if the model needs column names. ## Reading as_list `exp.as_list(label=1)` returns a list of `(description, weight)` tuples: | Part | What it is | |---|---| | description | the feature as a condition on this row: `lead_time > 120.00` for a binned continuous column, `fare_class=basic` for a categorical one with `categorical_names` | | weight | the surrogate's coefficient for that condition, on the predicted-probability scale of `label` | | order | sorted by absolute weight, largest first | | length | at most `num_features` (10 by default) | With the default `discretize_continuous=True`, each continuous column is replaced by an indicator: is this sample's value in the same quartile bin as the original row's? A weight of `+0.08` on `lead_time > 120.00` therefore reads: in the neighbourhood lime sampled, being in this row's top lead-time bin raised the surrogate's no-show probability by about 0.08 compared with samples where lead time fell in another bin. A negative weight pulls the probability down. The weights do **not** have to add up to the model's probability; how closely the surrogate matched the model is reported separately on the explanation. ## Which label you are reading The `label` argument of `as_list` defaults to `1`, matching `explain_instance`'s default `labels=(1,)`. For a binary model whose class 1 is "no-show", that is the class you want. Two traps: - With `explain_instance(..., top_labels=1)`, lime ignores `labels` and explains only the most probable class. When that class is 0, `as_list()` with its default `label=1` raises a `KeyError`, because no explanation was computed for class 1. - For a multi-class model, ask `exp.available_labels()` which labels were explained and pass one explicitly. `class_names` on the explainer changes only how classes are displayed; `as_list` still takes the integer index. ## Other views of the same result - `exp.as_map()` returns the raw dictionary `{label: [(column_index, weight), ...]}`, convenient for storing results from a batch job. - `exp.as_pyplot_figure(label=1)` draws a horizontal bar chart, green for positive and red for negative weights. - `exp.show_in_notebook()` renders the HTML view with the predicted probabilities and the row's feature values; `exp.save_to_file(path)` writes the same page. - `exp.predict_proba` keeps the model's probabilities for the original row, so the prediction can be shown beside its explanation. An answer that says "LIME tells you the model's feature importances" misses the point: these are local coefficients of a fitted stand-in, valid around one row and for one class. Two rows with the same top feature can carry very different weights for it, and a feature absent from `as_list` may simply have fallen below the `num_features` cut rather than mattering not at all. When the explanation goes to an analyst, show the model's probability from `exp.predict_proba` beside it, so the reader sees what is being explained before reading why.

  • What does lime's SubmodularPick add when analysts want a few representative explanations rather than one?
    `lime.submodular_pick.SubmodularPick(explainer, data, predict_fn, method='sample', sample_size=1000, num_exps_desired=5, num_features=10)` explains up to 1,000 sampled rows (every row with `method='full'`), then greedily picks the `num_exps_desired` explanations that together cover the most important features. The picks land in `sp_explanations`, all candidates in `explanations`. It costs one `explain_instance` per sampled row, and the row sample uses numpy's global generator, not the explainer's `random_state`.
  • How does as_list read for a model explained with mode='regression'?
    The `label` argument is ignored: lime explains the single output, and `as_list()` returns weights on the predicted value's scale, positive raising the prediction. `predict_fn` should return one prediction per row as a 1-D array, and `exp.predicted_value`, `min_value` and `max_value` feed the notebook view.

  • LIME weights are the model's own feature importances.
  • Passing model.predict works the same as predict_proba for a classifier.
  • as_list always describes the predicted class.
  • The weights in as_list add up to the predicted probability.
  • A positive weight on a binned feature is a per-unit slope.