Using SHAP and LIME for Model Interpretation

Tutorial 2 of 5

1. Introduction

Goal of the Tutorial

In this tutorial, we aim to understand two popular techniques for interpreting machine learning models, SHAP (SHapley Additive exPlanations) and LIME (Local Interpretable Model-Agnostic Explanations).

Learning Outcomes

By the end of this tutorial, you will:
- Understand what SHAP and LIME are
- Know how to interpret a machine learning model using SHAP and LIME
- Practical knowledge of implementing SHAP and LIME using Python

Prerequisites

A basic understanding of Python, machine learning models, and familiarity with Python's data science stack (Pandas, numpy, scikit-learn) is required. Prior exposure to Jupyter Notebooks would be beneficial.

2. Step-by-Step Guide

SHAP

SHAP connects optimal credit allocation with local explanations using the classic Shapley values from cooperative game theory and their related extensions.

It works by calculating the contribution of each feature to the prediction for each instance.

LIME

LIME explains predictions of any classifier or regressor in a faithful way by approximating it locally with an interpretable model.

It works by perturbing the instance, learning locally to approximate the underlying model.

3. Code Examples

Using SHAP

import shap
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
import pandas as pd
import numpy as np

# load dataset
data = pd.read_csv('data.csv')
X = data.drop('target', axis=1)
y = data['target']

# split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# train model
model = RandomForestRegressor()
model.fit(X_train, y_train)

# create explainer
explainer = shap.TreeExplainer(model)

# calculate shap values
shap_values = explainer.shap_values(X_test)

# plot
shap.summary_plot(shap_values, X_test)

Using LIME

import lime
from lime.lime_tabular import LimeTabularExplainer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np

# load dataset
data = pd.read_csv('data.csv')
X = data.drop('target', axis=1)
y = data['target']

# split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# train model
model = RandomForestClassifier()
model.fit(X_train, y_train)

# create explainer
explainer = LimeTabularExplainer(X_train.values, 
                                 feature_names=X_train.columns, 
                                 class_names=['0', '1'], 
                                 verbose=True, 
                                 mode='classification')

# explain a prediction
exp = explainer.explain_instance(X_test.values[0], model.predict_proba, num_features=5)
exp.show_in_notebook(show_all=False)

4. Summary

In this tutorial, we've covered how to use SHAP and LIME for interpreting machine learning models. We have also seen how to implement them in Python.

5. Practice Exercises

  1. Try to use SHAP and LIME with different machine learning models and datasets.
  2. Explore how different parameters of SHAP and LIME (like the number of features) affect the interpretation.
  3. Can you think of a way to integrate model interpretation into model selection?

Additional Resources

  1. Interpretable Machine Learning with Python
  2. SHAP Github Repo
  3. LIME Github Repo