Local explainability with LIME

Explainable AI in Python

Fouad Trad

Machine Learning Engineer

  • LIMELocal Interpretable Model-Agnostic Explanations
  • Explains predictions of complex models
  • Works on individual instances
  • Agnostic to model type

LIME logo.

Explainable AI in Python

Lime explainers

  • Tailored to different kinds of data

Diagram showing available LIME explainers. The first one is the LIME tabular explainers.

Explainable AI in Python

Lime explainers

  • Tailored to different kinds of data

Same diagram showing the Text Explainer additionally.

Explainable AI in Python

Lime explainers

  • Tailored to different kinds of data
  • Generates perturbations around a sample
  • Sees effect on model's output
  • Constructs simpler model for explanation

Same diagram showing the Image Explainer additionally.

Explainable AI in Python

Lime explainers

  • Tailored to different kinds of data
  • Generates perturbations around a sample
  • Sees effect on model's output
  • Constructs simpler model for explanation

Same diagram with the Tabular explainer highlighted.

Explainable AI in Python

Admissions dataset

GRE Score TOEFL Score University Rating SOP LOR CGPA Chance of Admit Accept
337 118 4 4.5 4.5 9.65 0.92 1
324 107 4 4 4.5 8.87 0.76 1
316 104 3 3 3.5 8 0.72 1
322 110 3 3.5 2.5 8.67 0.8 1
314 103 2 2 3 8.21 0.45 0

 

  • regressor: predicts chance of admit
  • classifier: predicts acceptance
  • Features in X
Explainable AI in Python

Creating tabular explainer

Regression
from lime.lime_tabular import LimeTabularExplainer

instance = X.iloc[1,:]
explainer_reg = LimeTabularExplainer( X.values,
feature_names=X.columns,
mode='regression'
)
explanation_reg = explainer_reg.explain_instance(
instance.values,
regressor.predict
)
Classification
from lime.lime_tabular import LimeTabularExplainer

instance = X.iloc[1,:]
explainer_class = LimeTabularExplainer( X.values,
feature_names=X.columns,
mode='classification'
)
explanation_class = explainer_class.explain_instance(
instance.values,
classifier.predict_proba
)
Explainable AI in Python

Visualizing explanation

Regression
explanation_reg.as_pyplot_figure()

Bar plot showing the effect of each feature on the regression model. Features are placed within a range for enhanced interpretability.

Classification
explanation_class.as_pyplot_figure()

Bar plot showing the effect of each feature on the classification model. Features are placed within a range for enhanced interpretability.

Explainable AI in Python

SHAP vs. LIME

SHAP
shap.waterfall_plot(...)

SHAP waterfall plot showing how each feature pushes the prediction positively or negatively.

LIME
explanation_class.as_pyplot_figure()

Bar plot showing the effect of each feature on the classification model. Features are placed within a range for enhanced interpretability.

Explainable AI in Python

Let's practice!

Explainable AI in Python

Preparing Video For Download...