A lime 0.2.0.1 tabular explanation shows exp.score near 0.3 and exp.local_pred far from the model's probability; what do they measure and what do you change?
answer
- how well the stand-in fitted
- weighted R squared on perturbed samples
- the stand-in's prediction at the row
- default ridge and automatic feature selection
- one value, overwritten per label
basics
~20 sexp.score is the weighted R squared of lime's ridge surrogate on its perturbed samples, and exp.local_pred is that surrogate's prediction for the row. A low score or a distant local_pred means the weights describe the model poorly around this row.
solid answer
~40 sInside `explain_instance`, lime fits `Ridge(alpha=1, fit_intercept=True)` by default on the `num_samples` perturbed rows, using the kernel weights and only the features chosen by `feature_selection`. `exp.score` is that fit's weighted R², `exp.local_pred` its prediction at the original row (a one-element array) and `exp.intercept[label]` its intercept; compare `local_pred` with `exp.predict_proba[label]`. In lime 0.2.0.1, `score` and `local_pred` are single attributes overwritten for each explained label, so they belong to the last one in the loop. For a low score, raise `num_features` (the default `'auto'` selection uses forward selection up to 6 features and highest weights above), compare against `feature_selection='none'` on a second explainer to see the ceiling, or revisit `kernel_width` and discretisation. Store the score with the weights rather than shipping weights alone.
code
python · 7 linesexp = explainer.explain_instance(
row, model.predict_proba, labels=(1,), num_features=6
)
print(exp.score) # weighted R^2 of the ridge surrogate
print(exp.local_pred) # surrogate's prediction at the row
print(exp.predict_proba[1]) # the model's probability for label 1
print(exp.intercept[1]) # surrogate intercept for label 1go deeper
Recall that score measures how well lime's linear stand-in fitted the sampled neighbourhood and local_pred is that stand-in's prediction for the row.
Explain the pipeline behind them: feature selection, a weighted Ridge(alpha=1) fit on num_samples rows, then the weighted R squared and a prediction at the original row.
Show you catch the release trap, one score per call in 0.2.0.1 with the last label winning, and that you store fit quality with every explanation and filter the poor fits.
Decide what fit quality a team requires before an explanation leaves the notebook, and who reviews the rows that fall below it.
## What lime fits inside one call Every `explain_instance` call on `lime.lime_tabular.LimeTabularExplainer` ends the same way. lime holds a matrix of perturbed samples (the first is the original booking), the model's predicted probability for each sample, and a proximity weight for each. It then: 1. picks at most `num_features` columns with the explainer's `feature_selection` method, 2. fits the **surrogate**, `model_regressor`, which defaults to `Ridge(alpha=1, fit_intercept=True)`, on those columns, weighting each sample by its proximity, 3. scores that fit on the same weighted samples, 4. predicts the original row with it. The weights in `as_list` are this ridge model's coefficients. Whether any surrogate can be faithful to a model locally is a general question about post-hoc explanations; what this package gives you is two numbers that say how well **this** fit went. ## The four numbers on the Explanation | Attribute | What it holds | Shape in 0.2.0.1 | |---|---|---| | `exp.local_exp[label]` | the `(column, coefficient)` pairs behind `as_list` | dict keyed by label | | `exp.intercept[label]` | the surrogate's intercept | dict keyed by label | | `exp.score` | weighted R² of the surrogate on the perturbed samples | one float | | `exp.local_pred` | the surrogate's prediction for the original row | one one-element array | `exp.predict_proba` holds the model's own probabilities for the row. For an airline's booking no-show model, if the model says 0.71 for "no-show" and `local_pred` says 0.42, the surrogate missed the very point it was meant to describe, and its coefficients should not be presented as the reason for 0.71. Setting `verbose=True` on the explainer prints the intercept, the local prediction and the model's value on every call. ## Which label the score belongs to in 0.2.0.1 `intercept` and `local_exp` are dictionaries with one entry per explained label, but in lime 0.2.0.1 `score` and `local_pred` are **single attributes** that the loop over labels overwrites: - with the default `labels=(1,)` there is one label, so they belong to label 1; - with `labels=(0, 1)` they end up describing label 1, the last in the loop; - with `top_labels=k`, lime loops from the least to the most probable of the top k, so they describe the most probable class. The GitHub master branch turned both into per-label dictionaries, but no release carries that change. On the installed package, explain one label per call when each label's fit quality matters. ## How feature selection shapes the fit `feature_selection` is a constructor argument and decides which columns the ridge model may use: | Value | What it does | |---|---| | `'auto'` (default) | `'forward_selection'` when `num_features <= 6`, otherwise `'highest_weights'` | | `'forward_selection'` | adds columns one at a time, each time keeping the one that most raises the weighted R²; costly for many features | | `'highest_weights'` | fits a ridge on all columns and keeps those with the largest absolute coefficient times the row's value | | `'lasso_path'` | walks the lasso path until at most `num_features` columns are non-zero | | `'none'` | uses every column and ignores `num_features` | A cap of 5 features on a 26-column model can leave real local structure out of the surrogate, and `score` drops accordingly. ## What to change when the score is low - **Raise `num_features`** on the call, then check whether `score` climbs. - **Fit once with `feature_selection='none'`** on a second explainer to see the best R² the surrogate reaches with every column. - **Swap the surrogate** with `model_regressor=` if the ridge penalty shrinks coefficients you need; any scikit-learn regressor with `coef_` and a `sample_weight` argument to `fit` works. - **Revisit `kernel_width` or the discretiser** on a new explainer. How width changes a surrogate is the local-surrogate topic's subject; here it is a constructor setting. - **Raise `num_samples`** if the score moves a lot between runs on the same row. A low score is not a verdict on the model. It says the linear stand-in fits the sampled neighbourhood poorly, so its coefficients deserve less trust. Store `score` and `local_pred` with every explanation a batch job writes, so a reviewer can filter out the poor fits. A practical rule is to flag any explanation whose `local_pred` sits further from `exp.predict_proba[label]` than the gap the readers would act on, and to send those rows for a closer look instead of publishing their weights unqualified.
- What does lime's feature_selection='auto' cost when num_features is 6 on 26 columns?It runs forward selection: at each step a ridge model is fitted for every remaining column, so 26 + 25 + 24 + 23 + 22 + 21 = 141 small weighted fits on the 5,000 samples, plus the final surrogate, per explained label. At 7 features `'auto'` switches to `'highest_weights'`, a single ridge fit on all columns, which is far cheaper.
- How do you get a per-label score for a three-class model on lime 0.2.0.1?Call `explain_instance` once per label with `labels=(k,)` and read `exp.score` and `exp.local_pred` after each call. A single call with `labels=(0, 1, 2)` keeps only label 2's values, because the attributes are overwritten in the loop; per-label dictionaries exist only on the unreleased master branch.
- exp.score is the model's accuracy on the explained row.
- local_pred is the model's own predicted probability.
- The installed lime stores score and local_pred per label.
- A low score proves the model itself is wrong.
- feature_selection='auto' always uses forward selection.
Assumes lime 0.2.0.1: score and local_pred are single values overwritten per label; per-label dictionaries exist only on the unreleased master branch.