0Pricing
Machine Learning Academy · درس

LIME: تفسيرات محلية قابلة للفهم ومستقلة عن النموذج

سيطبّق المتعلمون LIME لشرح مصنّف للصور ومصنّف للنصوص، وينشئون تقريبات خطية محلية توضّح وحدات البكسل أو الكلمات التي أثّرت في التنبؤ.

LIME: تفسيرات محلية قابلة للفهم ومستقلة عن النموذج درس مجاني في Machine Learning Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Machine Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Machine Learning Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

LIME: The Core Idea

LIME (Local Interpretable Model-Agnostic Explanations) explains individual predictions of any black-box model by approximating it locally with a simple, interpretable model. The insight is that even if a neural network or ensemble is globally complex, its behaviour near a single input can often be well-approximated by a linear model. LIME is model-agnostic: it works with any classifier or regressor through its prediction function alone.

The LIME Algorithm Step by Step

LIME follows three steps for each prediction to explain: (1) Perturb the input sample by creating many slightly modified copies; (2) Query the black-box model for predictions on all perturbed samples; (3) Fit a weighted linear model where samples closer to the original get higher weights. The coefficients of this linear surrogate are the explanation.

# Conceptual pseudocode for LIME
def lime_explain(instance, model, n_samples=5000):
    perturbed = perturb_around(instance, n=n_samples)
    predictions = model.predict_proba(perturbed)
    weights = kernel(distance(perturbed, instance))
    surrogate = LinearRegression()
    surrogate.fit(perturbed, predictions, sample_weight=weights)
    return surrogate.coef_  # these are the LIME explanations

Installing the lime Library

Install the lime package with pip install lime. The library provides specialised explainers for different data modalities: lime.lime_tabular.LimeTabularExplainer for structured tabular data, lime.lime_text.LimeTextExplainer for documents and sentences, and lime.lime_image.LimeImageExplainer for images. Each adapts the perturbation strategy to match the domain.

import lime
import lime.lime_tabular
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
    data.data, data.target, test_size=0.2, random_state=42
)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

LimeTabularExplainer for Tabular Data

Create a LimeTabularExplainer using the training data to learn the feature distributions for perturbation. Provide feature names, class names, and set discretize_continuous=True to bin continuous features into human-readable ranges like 'radius <= 13.1'. This makes the surrogate model coefficients directly readable as rule-like conditions.

explainer = lime.lime_tabular.LimeTabularExplainer(
    X_train,
    feature_names=list(data.feature_names),
    class_names=list(data.target_names),
    discretize_continuous=True,
    mode='classification'
)

# Explain a single test instance
instance = X_test[0]
explanation = explainer.explain_instance(
    instance,
    model.predict_proba,
    num_features=10,
    num_samples=5000
)

Reading the Tabular Explanation

The explanation lists feature conditions and their weights. A positive weight means the condition supports the predicted class; a negative weight means it contradicts it. For example, 'worst radius <= 16.8 (weight=0.32)' means that having a small worst radius strongly favours predicting the benign class. Call explanation.as_list() to get these as a Python list of (condition, weight) tuples.

# Print top features with their weights
for feature, weight in explanation.as_list():
    direction = 'supports' if weight > 0 else 'contradicts'
    print(f'{feature}: weight={weight:.3f} ({direction} predicted class)')

# Or display as a matplotlib bar chart
explanation.as_pyplot_figure(label=1)
import matplotlib.pyplot as plt
plt.tight_layout()
plt.savefig('lime_tabular.png', dpi=150)

LimeTextExplainer for Text Data

For text classification, LIME perturbs the input by randomly hiding words and observing how the prediction changes. Words that, when removed, cause the prediction to drop significantly receive high positive weights. This identifies the most influential words for the predicted label, making it easy to audit a spam filter or sentiment classifier for spurious patterns.

from lime.lime_text import LimeTextExplainer
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB

# Toy spam pipeline
text_pipeline = Pipeline([
    ('tfidf', TfidfVectorizer()),
    ('clf', MultinomialNB())
])
reviews = ['great product fast shipping', 'buy now click here free money']
labels = [0, 1]  # 0=ham, 1=spam
text_pipeline.fit(reviews, labels)

text_explainer = LimeTextExplainer(class_names=['ham', 'spam'])
exp = text_explainer.explain_instance(
    'free fast money click',
    text_pipeline.predict_proba,
    num_features=6
)
print(exp.as_list())

LimeImageExplainer for Images

For images, LIME segments the image into superpixels (coherent regions) using SLIC and randomly masks groups of superpixels. The linear surrogate identifies which superpixels most influenced the prediction. This reveals whether the CNN is looking at the dog's face or the grass background. Call get_image_and_mask to produce an annotated overlay.

from lime.lime_image import LimeImageExplainer
from skimage.segmentation import mark_boundaries
import numpy as np

image_explainer = LimeImageExplainer()

# Assume 'img' is a (224, 224, 3) numpy array and 'cnn_predict' wraps the model
# exp = image_explainer.explain_instance(
#     img, cnn_predict, top_labels=1, hide_color=0, num_samples=1000
# )
# image, mask = exp.get_image_and_mask(
#     exp.top_labels[0], positive_only=True, num_features=5
# )
# plt.imshow(mark_boundaries(image, mask))
print('Image explainer segments image into superpixels and identifies key regions.')

LIME vs SHAP: Key Differences

Both LIME and SHAP explain individual predictions, but they differ in important ways. LIME is faster, more customisable, and works naturally with text and images, but its explanations can vary between runs because perturbation is random. SHAP provides mathematically consistent explanations with the efficiency axiom, but TreeSHAP is only exact for tree models and KernelSHAP can be slow on large feature sets.

# Summary comparison
comparison = {
    'LIME': {
        'consistency': 'Random — varies between runs',
        'speed': 'Moderate (5k perturbations)',
        'modalities': 'Tabular, text, image',
        'axioms_satisfied': False
    },
    'SHAP': {
        'consistency': 'Exact for TreeExplainer',
        'speed': 'Fast (TreeSHAP), slow (KernelSHAP)',
        'modalities': 'Tabular, text, image (DeepSHAP)',
        'axioms_satisfied': True
    }
}
for method, props in comparison.items():
    print(method, props)

Stability: Increasing num_samples

LIME's explanations are stochastic because they depend on random perturbations. Increasing num_samples from the default 5000 reduces variance at the cost of more model calls. To test stability, run explain_instance multiple times with the same input and compare the top-5 feature weights. If rankings shift dramatically, increase num_samples or reduce the neighbourhood width via the kernel_width parameter.

import numpy as np

weights_runs = []
for seed in range(5):
    np.random.seed(seed)
    exp = explainer.explain_instance(
        instance, model.predict_proba,
        num_features=5, num_samples=5000
    )
    weights_runs.append(dict(exp.as_list()))

# Check variance across runs for top feature
top_feature = exp.as_list()[0][0]
vals = [run.get(top_feature, 0) for run in weights_runs]
print(f'Top feature weight std across 5 runs: {np.std(vals):.4f}')

Using Explanations to Detect Model Bugs

LIME is a powerful debugging tool. If a sentiment classifier marks a review positive because of the word 'not' (the negation that should flip sentiment), LIME will reveal the word 'not' as a strong positive contributor — exposing the bug. Similarly, a medical classifier relying on 'hospital name' rather than clinical features can be caught by LIME before it enters production.

# Example: checking a suspicious prediction
suspect_text = 'I do not recommend this product at all'
exp = text_explainer.explain_instance(
    suspect_text,
    text_pipeline.predict_proba,
    num_features=5
)
for word, weight in exp.as_list():
    print(f'  word={word!r:15} weight={weight:+.3f}')
# If 'not' has positive weight for positive sentiment class,
# the model has learned a spurious pattern.

Integrating LIME into a Review Workflow

A practical pattern is to run LIME automatically on all high-confidence but counter-intuitive predictions: cases where the model is very confident but the ground truth label is wrong, or where a domain expert disagrees. Logging these explanations alongside predictions creates an audit trail for compliance and helps identify systematic biases before they affect users.

# Automated explanation logging for high-confidence errors
for i, (pred, true) in enumerate(zip(model.predict(X_test), y_test)):
    confidence = model.predict_proba(X_test[i:i+1]).max()
    if pred != true and confidence > 0.9:
        exp = explainer.explain_instance(
            X_test[i], model.predict_proba, num_features=5
        )
        print(f'Sample {i}: predicted={data.target_names[pred]}, '
              f'true={data.target_names[true]}, conf={confidence:.2f}')
        for feat, w in exp.as_list()[:3]:
            print(f'  {feat}: {w:+.3f}')

Quick Check

Test your understanding of Machine Learning with Python concepts from this lesson.

Lesson Recap

In this lesson you learned: LIME approximates any black-box model locally with a weighted linear surrogate by perturbing the input and querying predictions, specialised explainers handle tabular, text, and image data with domain-appropriate perturbation strategies, and LIME can expose model bugs and biases by revealing which features or words drove a specific prediction. Next up we examine fairness metrics to measure and audit discriminatory model behaviour.

الأسئلة الشائعة

هل درس «LIME: تفسيرات محلية قابلة للفهم ومستقلة عن النموذج» مجاني؟

نعم — نص درس «LIME: تفسيرات محلية قابلة للفهم ومستقلة عن النموذج» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Machine Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Machine Learning Academy 4 دروس في المجموع.

ماذا ستتعلم في «LIME: تفسيرات محلية قابلة للفهم ومستقلة عن النموذج»؟

سيطبّق المتعلمون LIME لشرح مصنّف للصور ومصنّف للنصوص، وينشئون تقريبات خطية محلية توضّح وحدات البكسل أو الكلمات التي أثّرت في التنبؤ. تتمرن على Machine Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Machine Learning Academy؟

لا تُشترط خبرة سابقة. Machine Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «LIME: تفسيرات محلية قابلة للفهم ومستقلة عن النموذج»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Machine Learning Academy هذا؟

نعم. كل درس في Machine Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. قيم SHAP: الأهمية العامة والمحلية للميزات
  2. LIME: تفسيرات محلية قابلة للفهم ومستقلة عن النموذج
  3. مقاييس الإنصاف: التكافؤ الديموغرافي وتكافؤ الفرص
  4. استراتيجيات الحد من التحيز: المعالجة المسبقة وأثناء المعالجة وما بعدها
← العودة إلى Machine Learning Academy