🤖 AI/ML

LIME

Local Interpretable Model-agnostic Explanations

A technique for explaining model predictions locally. Interprets black-box models.

📖 Detailed Explanation

LIME (Local Interpretable Model-agnostic Explanations) is an XAI (Explainable AI) technique that interprets individual predictions of black-box models. Published by Ribeiro et al. in 2016, it explains "why" a specific prediction came out the way it did, regardless of the type of model (Model-agnostic).

Core idea: Even a complex global model can be approximated by a simple linear model around a specific data point. LIME creates perturbations (variations) around the sample to be explained and learns the decision boundary of the local region.

How it works: 1) Generate perturbations around the original sample, 2) Obtain the prediction of each perturbation from the black-box model, 3) Assign weights based on distance from the original, 4) Extract feature importance with weighted linear regression. This process enables intuitive explanations such as "this image is a cat because of the ears and eyes."

In practice, LIME is essential in fields with an explanation obligation, such as financial screening, medical diagnosis, and legal AI. The EU AI Act also requires explainability for high-risk AI, and LIME has become established as a standard tool for meeting this.

💻 Code Example

import lime
import lime.lime_tabular
import lime.lime_text
import numpy as np
from sklearn.ensemble import RandomForestClassifier

# 1. 테이블 데이터 설명 (대출 심사 예시)
X_train = np.array([...])  # 특성: 소득, 부채비율, 신용점수 등
model = RandomForestClassifier().fit(X_train, y_train)

explainer = lime.lime_tabular.LimeTabularExplainer(
    X_train,
    feature_names=['소득', '부채비율', '신용점수', '근무년수', '대출금액'],
    class_names=['승인', '거절'],
    mode='classification'
)

# 특정 고객의 거절 이유 설명
idx = 42  # 거절된 고객
exp = explainer.explain_instance(
    X_test[idx],
    model.predict_proba,
    num_features=5,
    num_samples=1000  # perturbation 수
)

# 결과: 거절에 기여한 요인
print(exp.as_list())
# [('부채비율 > 0.4', 0.35), ('신용점수 < 650', 0.28), ...]
exp.show_in_notebook()  # 시각화

# 2. 텍스트 데이터 설명 (스팸 분류)
from lime.lime_text import LimeTextExplainer

text_explainer = LimeTextExplainer(class_names=['정상', '스팸'])
text = "무료 당첨! 지금 클릭하세요 100만원 즉시 지급"

exp = text_explainer.explain_instance(
    text,
    spam_model.predict_proba,
    num_features=10
)
# 결과: '무료', '당첨', '클릭'이 스팸 판정에 기여
exp.as_pyplot_figure()

🗣️ Say It Like This in Practice

💬 In a Meeting
"We have to explain to the customer whose loan was rejected why it was rejected, but since it's an XGBoost model, it can't be explained directly. I'll attach LIME and automatically generate a report saying that 'debt ratio over 40%' and 'credit score below 650' are the main factors."
💬 In an Interview
"I have experience interpreting an image classification model with LIME. After superpixel segmentation, I measured the change in prediction while masking each region. In the cat image, the ear region was the most important, with a 70% contribution."
💬 In a Technical Discussion
"Comparing LIME vs SHAP, LIME is fast because it is a local linear approximation, but it has sampling variability. SHAP is based on Shapley values, so it is theoretically more robust, but its computational cost is high. We use LIME for real-time explanations and SHAP for detailed analysis."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Setting num_samples too low

With few samples, the variance of the explanation is large, so the same input can produce different explanations. A minimum of 1000 or more is recommended.

❌
Mistaking LIME results for global interpretation

LIME is a local explanation of a specific sample and does not explain the behavior of the entire model. For global interpretation, use SHAP summary plots or PDP.

✅
The right way

Adjust the size of the local region with the kernel_width parameter. For images, adjusting the number of superpixels is important, and for text, adjusting at the word/sentence level is important.

🔗 Related Terms

📚 Learn More