skip to content

In lime's LimeTabularExplainer, what do categorical_features and discretize_continuous change about an explanation, and how must categorical columns be encoded?

level: middleimportance: should knowfreq 11%

answer

  1. categories drawn from training frequencies
  2. integer codes in, names via a map
  3. continuous columns binned by default
  4. quartile, decile or entropy bins
  5. one discretiser needs the labels

basics

~20 s

categorical_features lists integer-coded columns that lime samples from their training frequencies and explains as column=value. discretize_continuous=True, the default, bins continuous columns at training quartiles, so a weight describes being in the row's bin, not a per-unit slope.

solid answer

~40 s

`categorical_features` takes column indices; lime's docstring requires integer values in those columns, and `categorical_names={index: [names]}` maps the codes to readable labels. lime samples each categorical column from its training frequencies and gives the surrogate a 0/1 feature, "equals this row's value", shown as `fare_class=basic`. With `discretize_continuous=True` (the default) and `discretizer='quartile'`, every continuous column is cut at its training quartiles; samples are drawn bin by bin and the explanation reads `lead_time > 120.00`, a weight for being in that bin. `'decile'` gives ten bins, while `'entropy'` fits shallow trees to `training_labels` and raises `ValueError` without them. With `discretize_continuous=False`, continuous columns are sampled from a normal scaled by their training standard deviation, and their weights are per standard deviation of the feature.

go deeper

for a junior

Recall that categorical columns must be integer-coded and listed by index in categorical_features, and that continuous columns are binned into quartiles unless discretize_continuous is turned off.

for a middle

Explain how sampling differs: categories drawn from training frequencies and seen as same-as-row indicators, continuous columns binned, sampled per bin and mapped back to numbers before the model sees them.

for a senior

Show you can pick the representation for the audience: bins for readable conditions, standardised weights for slopes, and a predict_fn that accepts lime's integer-coded array.

for a principal

Argue how the encoding choice shapes what the business may conclude, and make the bin edges and category mapping part of the stored explanation's record.

## Two kinds of columns `lime.lime_tabular.LimeTabularExplainer` treats each column of the training array as either **categorical** or **continuous**. That decision changes three things: how perturbed samples are drawn, what the surrogate model sees, and how the explanation is worded. Two constructor arguments make it: `categorical_features` and `discretize_continuous`. Take an airline's booking no-show model with 26 features, 7 of them categorical (fare class, booking channel, cabin and so on) and the rest numeric (lead time, party size, past no-shows). ## Categorical columns: integers in, names out `categorical_features` is a list of column **indices**. lime's docstring says the values in those columns must be integers, and the code converts the row's value with `int(...)` to build the feature's name. So the 7 categorical columns must be integer-coded before they reach lime: encode them once and keep the mapping. - `categorical_names` is a dict from column index to a list of names, so code 2 in the fare-class column can read `fare_class=basic` instead of `fare_class=2`. - For each categorical column, lime samples values from the column's **training frequencies**, so a rare category is rarely drawn. - The surrogate does not see the code itself. It sees a 0/1 feature: does this sample have the same value as the row being explained? Its weight is the effect of keeping the row's category rather than any other. ## Continuous columns: binned by default `discretize_continuous=True` is the default. Every column not listed as categorical is cut into bins computed from the training data, and from then on lime treats **all** columns as categorical: samples are drawn bin by bin from the training frequencies, and each sampled bin is turned back into a number, drawn from a truncated normal inside the bin, before `predict_fn` sees it. | `discretizer` | Bins | Note | |---|---|---| | `'quartile'` (default) | 4, cut at the 25th, 50th and 75th percentiles | names like `lead_time <= 14.00` or `14.00 < lead_time <= 45.00` | | `'decile'` | 10 | finer bins, more possible conditions | | `'entropy'` | up to 8 per column, from a depth-3 decision tree fitted to the labels | needs `training_labels`; raises `ValueError` without them | | a `BaseDiscretizer` instance | your own | any other value raises `ValueError` | The explanation now reads in conditions. A weight of `+0.06` on `lead_time > 120.00` means that being in this row's lead-time bin raised the surrogate's probability; it does not mean each extra day did. ## Turning discretisation off With `discretize_continuous=False`, continuous columns stay numeric: - samples come from a normal distribution scaled by each column's training standard deviation, centred on the training mean, or on the row itself with `sample_around_instance=True`; - the surrogate is fitted on standardised values, so a continuous feature's weight is per **training standard deviation** of that feature; - the description is just the column name, which some readers find harder to act on than a bin. Discretisation is also skipped when the training data is a sparse matrix. ## What the encoding means for predict_fn lime hands `predict_fn` arrays in the same encoding as `training_data`: integer codes in the categorical columns, numbers elsewhere. If the model was fitted on strings, a one-hot layout or a DataFrame, `predict_fn` must translate: ```python def predict_fn(X): frame = pd.DataFrame(X, columns=feature_names) frame[cat_cols] = frame[cat_cols].astype(int) return model.predict_proba(frame) ``` ## A setup checklist 1. Integer-encode the categorical columns and pass their indices as `categorical_features`. 2. Pass `categorical_names` so the explanation shows category names, not codes. 3. Keep `discretize_continuous=True` when readers want conditions; choose `'decile'` for finer bins, or `'entropy'` together with `training_labels`. 4. Turn discretisation off when weights per standard deviation suit the audience better. 5. Make `predict_fn` accept lime's integer-coded array. Whether sampling each column independently produces realistic bookings is a question about the faithfulness of perturbation explanations in general; on this package, the settings above decide how the samples are drawn. Record them with every stored explanation: the same booking explained with quartile bins, decile bins or no discretisation produces three differently worded lists, and a reviewer comparing explanations across months needs to know which one each came from. The bin edges and the category mapping are part of what an explanation means, not incidental configuration.

  • Where do lime's quartile bin edges come from, and when are they computed?
    From the `training_data` passed to `LimeTabularExplainer`: the 25th, 50th and 75th percentiles of each continuous column, computed once when the explainer is built. Rows from a later period are still explained against those edges, so rebuild the explainer when the training snapshot changes, and store the edges with the explanations.
  • Why can two similar bookings get lime weights that are not comparable for the same continuous feature?
    With quartile discretisation, the feature is "in the same bin as this row". Two rows on either side of a cut point are explained with different conditions, such as `lead_time > 120.00` against `45.00 < lead_time <= 120.00`, so their weights describe different bins, not one slope. Check the conditions before comparing rows.

  • Categorical columns can be passed to lime as strings.
  • A weight on lead_time > 120.00 is a per-day effect.
  • discretize_continuous only changes how the explanation is printed.
  • The entropy discretizer works without training labels.
  • lime samples every category with equal probability.