🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #225

LIME

LIME (Local Interpretable Model-agnostic Explanations) explains a single prediction by fitting a simple, interpretable model — usually linear — to approximate the complex model's behavior just in the local neighborhood around that one prediction.

The Core Idea

StepWhat Happens
1Pick the specific instance you want to explain
2Generate many perturbed variations of that instance (small random changes to its feature values)
3Get the complex model's prediction for every perturbed variation
4Fit a simple, interpretable model (usually linear regression) to these perturbed points, weighted by proximity to the original instance
5The simple model's coefficients become the explanation — locally accurate, even if the simple model would be a poor fit globally

Why "Local" Is the Whole Point

A complex model's overall decision boundary might be wildly non-linear across the full feature space — but zoomed into a small neighborhood around one specific point, it often looks approximately linear (the same intuition behind local linear approximation in calculus). LIME exploits exactly this: it doesn't need the simple surrogate model to explain the whole complex model, just the tiny region right around the instance being explained.

Python Implementation

import lime
import lime.lime_tabular

explainer = lime.lime_tabular.LimeTabularExplainer(
    X_train.values,
    feature_names=X_train.columns.tolist(),
    class_names=["not fraud", "fraud"],
    mode="classification",
)

instance = X_test.iloc[0]
explanation = explainer.explain_instance(instance.values, model.predict_proba, num_features=5)
explanation.show_in_notebook()
print(explanation.as_list())   # e.g. [('income <= 40000', 0.23), ('credit_score > 700', -0.15), ...]

Expected output: a ranked list of feature conditions and their local contribution to this one specific prediction — "income <= 40000" contributing +0.23 toward the "fraud" prediction, for example, specifically for this instance, not necessarily generalizable to every other instance in the dataset.

LIME vs SHAP

LIMESHAP
Theoretical foundationHeuristic — fits a local linear approximationGame-theoretic — Shapley values with proven mathematical properties
Consistency guaranteesNone formally guaranteedEfficiency, symmetry, and other provable properties
SpeedGenerally fasterCan be slower, especially with the model-agnostic explainer
ScopeLocal onlyBoth local and global

SHAP's mathematical guarantees make it the more rigorous, generally preferred choice today — LIME remains useful for its speed and conceptual simplicity, and as a second, independent check against SHAP's explanations.

Practical Use Cases

  • Fast, single-prediction explanations, especially useful during initial model debugging
  • A model-agnostic technique when a model-specific SHAP explainer (like TreeExplainer) isn't available

Limitations

  • Results can be somewhat unstable — different runs (different random perturbations) can produce noticeably different explanations for the same instance
  • No formal mathematical guarantees the way SHAP's Shapley-value foundation provides
  • Purely local — doesn't directly produce a global feature importance ranking the way SHAP can

Common Mistakes

  • Treating a single LIME explanation as fully stable and definitive, without checking whether it's consistent across a few repeated runs.
  • Using LIME when a global explanation was actually needed — it's fundamentally a local, per-instance technique.

Interview Relevance

Q: "Why might two runs of LIME on the same prediction give slightly different explanations?" LIME generates random perturbations around the instance and fits a local surrogate model to them — different random perturbations can produce a somewhat different local linear fit, unlike SHAP's Shapley values, which are mathematically well-defined and consistent for the same model and instance.

Practice Question

Explain, in plain language, why LIME's local linear surrogate model can still produce a useful explanation even when the underlying complex model's overall decision boundary is highly non-linear.

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

LIME – FAQs

Quick answers about learning LIME in Machine Learning.

This free note from CodingNow 2.0 explains LIME in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including LIME, is 100% free with no signup required.
With focused practice, most students grasp LIME in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now