WIA-AI-009 | Chapter 4

LIME and Local Explanations

Introduction to LIME

LIME (Local Interpretable Model-agnostic Explanations), introduced by Marco Ribeiro, Sameer Singh, and Carlos Guestrin in 2016, is a representative model-agnostic technique that explains individual predictions through local approximations. The central insight is elegant: even if a model is globally complex, we can understand its behavior locally—in the neighborhood of a specific prediction—using a simple, interpretable model.

Think of it like understanding Earth's curvature. Globally, Earth is a sphere, which is complex to describe mathematically. But locally—in your neighborhood—it appears flat, which is much simpler. LIME applies this same principle to machine learning: approximate a complex model locally with a simple linear model that's easy to understand.

弘益人間 Principle: LIME democratizes AI explainability by working with any model type, making transparency accessible regardless of the underlying technology. This universality serves humanity by ensuring no one is excluded from understanding AI decisions.

The LIME Algorithm

LIME operates through a four-step process that creates a local linear approximation around a specific prediction:

Step 1: Perturbation - Creating Synthetic Neighbors

Given an instance you want to explain, LIME generates thousands of synthetic "neighbors" by randomly perturbing features. For tabular data, this might mean randomly changing feature values while keeping them realistic. For images, it might mean turning superpixels on or off.

Example: Loan Application
Original instance: {credit_score: 720, debt_ratio: 0.35, employment_years: 8}

Perturbed instances:
1. {credit_score: 685, debt_ratio: 0.35, employment_years: 8}
2. {credit_score: 720, debt_ratio: 0.42, employment_years: 8}
3. {credit_score: 745, debt_ratio: 0.35, employment_years: 6}
... (generate 5,000 such perturbations)

Step 2: Prediction - Query the Black Box

Feed all perturbed instances through the black-box model to get predictions. LIME doesn't need to know how the model works internally—it just needs input-output pairs.

Step 3: Weighting - Emphasize Nearby Points

Weight each perturbed instance by its distance from the original. Closer instances get higher weights because they're more relevant for understanding local behavior. This ensures the linear model focuses on the immediate neighborhood, not distant regions where the approximation might be poor.

Step 4: Linear Model Fitting

Fit a weighted linear regression model using the perturbed instances as training data. The coefficients of this linear model become your explanation—they show how each feature locally affects the prediction.

// LIME algorithm pseudocode (WIA-AI-009)
function explainWithLIME(instance, model, numSamples=5000) {
  const perturbations = [];
  const predictions = [];
  const weights = [];

  // Generate perturbations
  for (let i = 0; i < numSamples; i++) {
    const perturbed = perturbInstance(instance);
    perturbations.push(perturbed);

    // Get model prediction
    predictions.push(model.predict(perturbed));

    // Weight by distance to original
    const distance = computeDistance(instance, perturbed);
    weights.push(kernelWeight(distance));
  }

  // Fit weighted linear model
  const linearModel = fitWeightedLinearRegression(
    perturbations,
    predictions,
    weights
  );

  // Coefficients are the explanation
  return linearModel.coefficients;
}

LIME for Different Data Types

LIME's model-agnostic nature extends across data modalities, but each requires domain-specific adaptations:

Tabular LIME

For structured data with numeric and categorical features:

Image LIME

Explaining image classifications presents unique challenges—individual pixels are too fine-grained to be interpretable. LIME addresses this through superpixel segmentation:

  1. Segment the image into superpixels (coherent regions of similar color/texture)
  2. Treat each superpixel as a binary feature (present or absent)
  3. Generate perturbations by randomly turning superpixels on (original) or off (gray/blur)
  4. Fit a linear model predicting class probability from superpixel presence
  5. Highlight superpixels with positive coefficients as "supporting" the prediction
Image Classification Example:
Model predicts: "Husky" (95% confidence)
LIME explanation: Highlights the dog's face, pointy ears, and blue eyes as supporting evidence.
Removes background snow, which doesn't contribute to classification.

This reveals the model correctly focuses on relevant features rather than spurious correlations.

Text LIME

For text classification and sentiment analysis:

Sentiment Analysis:
Text: "The movie was absolutely terrible, but the acting was superb."
Model prediction: Negative (65%)
LIME explanation:
+ "terrible" (+0.45) strongly supports negative
- "superb" (-0.28) contradicts negative
+ "but" (+0.12) introduces contrast, supports negative
Other words have minimal impact

Key Parameters and Tuning

LIME's effectiveness depends on proper parameter configuration:

Number of Samples

How many perturbations to generate. More samples improve accuracy but increase computation time.

Kernel Width

Controls the "locality" of the explanation—how quickly weights decay with distance. Smaller widths create tighter, more local approximations. Larger widths incorporate broader context but may miss local nonlinearity.

The WIA-AI-009 standard recommends: kernel_width = sqrt(num_features) × 0.75 as a starting point, then tune based on validation.

Number of Features in Explanation

Limit explanations to the top K most important features (typically 5-10) to avoid overwhelming users with information. More features provide completeness, fewer provide clarity.

Perturbation Strategy

How to generate realistic perturbations:

LIME vs. SHAP: A Comparison

Both LIME and SHAP provide local explanations, but they differ in approach and guarantees:

Aspect LIME SHAP
Theoretical Foundation Local linear approximation Game theory (Shapley values)
Consistency (monotonicity) axiom Not guaranteed Provably satisfied
Additivity Approximate Exact (sum = prediction - baseline)
Speed About 1,800ms on average in the specification's benchmarks Varies by variant (specification benchmarks: TreeSHAP about 45ms, KernelSHAP about 2,500ms)
Stability Variable (depends on sampling) More stable
Interpretability Linear model, very intuitive Contribution scores, slightly less intuitive
Model-agnostic Yes Yes (but specialized versions faster)

When to Use LIME

When to Use SHAP

Implementing LIME in Production

Deploying LIME effectively requires attention to several practical considerations:

WIA-AI-009 LIME Implementation

import { TabularLIMEExplainer } from 'wia-ai-009';

// Initialize explainer
const explainer = new TabularLIMEExplainer({
  featureNames: ['credit_score', 'debt_ratio', 'employment_years'],
  numSamples: 5000,
  kernelWidth: Math.sqrt(3) * 0.75,
  numFeatures: 5
});

// Explain a prediction
const instance = {
  credit_score: 720,
  debt_ratio: 0.35,
  employment_years: 8
};

const explanation = await explainer.explain(
  instance,
  (x) => model.predict(x)  // Black-box model function
);

// WIA-AI-009 standardized output
console.log(explanation);
// {
//   "standard": "WIA-AI-009",
//   "explanation_type": "local",
//   "method": "lime",
//   "instance": {...},
//   "prediction": 0.32,
//   "intercept": 0.15,
//   "feature_weights": {
//     "debt_ratio": 0.42,
//     "credit_score": -0.28,
//     "employment_years": -0.15
//   },
//   "r_squared": 0.89,
//   "num_samples_used": 5000
// }

Caching Strategies

LIME requires running thousands of model predictions per explanation, which can be expensive. Caching helps:

Validation and Quality Metrics

How do we know if a LIME explanation is good? The WIA-AI-009 standard defines quality metrics:

Advanced LIME Techniques

Anchor Explanations

Proposed by the LIME authors in follow-up work (2018), Anchor explanations provide rule-based explanations with coverage guarantees. For example: "IF credit_score > 700 AND debt_ratio < 0.40 THEN approve (95% precision, 85% coverage)"

Submodular Pick LIME (SP-LIME)

Instead of explaining individual instances, SP-LIME selects a small set of representative instances that, when explained together, provide global understanding of the model. This bridges local and global explainability.

Constrained LIME

Incorporate domain knowledge as constraints: "Age cannot be negative," "Sum of probabilities = 1," etc. This ensures perturbations remain realistic and explanations respect known relationships.

Limitations and Pitfalls

Instability

Because LIME uses random sampling, running it twice on the same instance can yield different explanations. This randomness can confuse users who expect consistency. Mitigation: use large sample counts and set random seeds for reproducibility.

Locality Calibration

Choosing the right kernel width is tricky. Too small, and the linear approximation fits perfectly but only over a tiny region that may not generalize. Too large, and the linear model fails to capture local nonlinearity.

Unrealistic Perturbations

Naive perturbation can create impossible instances: "Age = 25, Years of Employment = 40." Such instances confuse the model and degrade explanation quality. Solution: use domain-aware perturbation strategies.

Feature Correlation

When features are correlated, perturbing them independently creates unrealistic combinations. For example, randomly varying house size and number of bedrooms independently might generate "1,000 sq ft house with 10 bedrooms." Advanced LIME implementations handle this by learning and respecting feature correlations.

LIME for Fairness Auditing

LIME can help detect bias and unfairness in models:

Demographic Parity Testing

Generate LIME explanations for instances that differ only in protected attributes (race, gender, etc.). If these attributes receive high weights, the model may be discriminating improperly.

Counterfactual Fairness

Use LIME to identify what changes would flip a decision. If the changes involve protected attributes, this signals potential fairness issues.

Disparate Impact Analysis

Compare LIME explanations across demographic groups. Do features have different importance for different groups? This could indicate the model treats groups differently, potentially unfairly.

Integration with WIA-AI-009 Protocol

The WIA-AI-009 standard defines a protocol for requesting and receiving LIME explanations:

// Explanation request (WIA-AI-009 Phase 3, XAI-REQUEST-v1)
{
  "protocol": "XAI-REQUEST-v1",
  "request_id": "uuid-v4",
  "timestamp": "2025-01-20T15:44:58Z",
  "model_identifier": {
    "model_id": "loan_classifier_v2.3",
    "version": "2.3.1",
    "deployment": "production"
  },
  "input_data": {
    "credit_score": 720,
    "debt_ratio": 0.35,
    "employment_years": 8
  },
  "explanation_config": {
    "methods": ["lime"],
    "num_features": 5,
    "target_audience": "technical"
  },
  "quality_requirements": {
    "min_fidelity": 0.85,
    "min_stability": 0.80,
    "max_computation_time_ms": 5000
  }
}
// The sample count (5,000) and kernel width (sqrt(number of features) × 0.75) are set in the explainer configuration.

// Explanation response (WIA-AI-009 Phase 3, XAI-RESPONSE-v1)
{
  "protocol": "XAI-RESPONSE-v1",
  "request_id": "uuid-v4",
  "response_id": "uuid-v4",
  "timestamp": "2025-01-20T15:45:00Z",
  "status": "success",
  "prediction": {
    "value": 0.32,
    "class": "low_risk",
    "confidence": 0.87
  },
  "explanations": [
    {
      "method": "lime",
      "explanation": {
        "intercept": 0.15,
        "feature_weights": {
          "debt_ratio": 0.42,
          "credit_score": -0.28,
          "employment_years": -0.15
        }
      },
      "quality_metrics": {
        "fidelity": 0.91,
        "r_squared": 0.89
      }
    }
  ],
  "metadata": {
    "computation_time_ms": 1250,
    "model_version_used": "2.3.1"
  }
}

Chapter Summary

LIME (Local Interpretable Model-agnostic Explanations) provides an elegant approach to explaining complex models through local linear approximations. By perturbing an instance, querying the black-box model, and fitting a weighted linear model, LIME creates interpretable explanations that reveal what features drove a specific prediction.

LIME's model-agnostic nature makes it universally applicable across tabular, image, and text data. For images, superpixel-based perturbations highlight which regions influenced classification. For text, word removal identifies sentiment-driving phrases. This versatility makes LIME accessible regardless of model architecture.

While LIME lacks the theoretical guarantees of SHAP (the consistency axiom, exact additivity), it offers practical advantages: relatively lightweight computation, highly interpretable linear explanations, and excellent support for images and text. The choice between LIME and SHAP depends on whether you prioritize speed and simplicity (LIME) or theoretical rigor and axiomatic guarantees (SHAP).

Effective LIME deployment requires careful parameter tuning: sufficient samples for stable explanations, appropriate kernel width for meaningful locality, and domain-aware perturbation strategies to avoid unrealistic instances. The WIA-AI-009 standard provides guidelines for these parameters and defines quality metrics (R², fidelity, stability) to validate explanation quality.

LIME's limitations—instability from random sampling, sensitivity to kernel width, challenges with correlated features—are manageable through proper engineering. Advanced techniques like Anchor explanations and SP-LIME extend LIME's capabilities for rule-based explanations and global model understanding.

Review Questions

  1. Explain the four-step LIME algorithm. Why is weighting by distance important?
  2. How does LIME adapt to different data types (tabular, image, text)? What changes for each?
  3. What is a superpixel, and why does Image LIME use superpixels rather than individual pixels?
  4. Compare LIME and SHAP across three dimensions: theoretical foundation, guarantees, and practical performance.
  5. What does the kernel width parameter control in LIME? What happens if it's too small or too large?
  6. Why might LIME produce different explanations when run twice on the same instance? How can this be mitigated?
  7. What is the R² score in the context of LIME, and why is it a useful quality metric?
  8. Describe two ways LIME can be used for fairness auditing.
  9. What are "unrealistic perturbations" and why are they problematic? Give an example.
  10. When would you choose LIME over SHAP? When would you choose SHAP over LIME?