Intermediate

SHAP Values

SHapley Additive exPlanations provide a unified approach to interpreting predictions based on game theory. Learn to compute and visualize SHAP values for any model.

What are SHAP Values?

SHAP (SHapley Additive exPlanations) is a game-theoretic approach to explain predictions. It assigns each feature an importance value for a particular prediction. The concept is rooted in Shapley values from cooperative game theory, which fairly distribute a "payout" (the prediction) among "players" (the features).

Key properties that make SHAP values theoretically grounded:

  • Local accuracy: SHAP values sum to the difference between the prediction and the average prediction.
  • Missingness: Features that are missing have no impact (SHAP value of 0).
  • Consistency: If a feature's contribution increases, its SHAP value never decreases.

Installing SHAP

Bash — Installation
pip install shap

# For Jupyter notebook visualization support
pip install shap[plots]

TreeExplainer

TreeExplainer computes exact SHAP values for tree-based models (XGBoost, LightGBM, Random Forest, Gradient Boosting) in polynomial time. It is the fastest and most accurate explainer for tree models.

Python — SHAP with XGBoost
import shap
import xgboost as xgb
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split

# Train a model
X, y = load_boston(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = xgb.XGBRegressor(n_estimators=100).fit(X_train, y_train)

# Create TreeExplainer - exact and fast for tree models
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test)

# Summary plot - global feature importance
shap.summary_plot(shap_values, X_test)

# Force plot - explain a single prediction
shap.force_plot(explainer.expected_value, shap_values[0], X_test[0])

# Dependence plot - feature interaction
shap.dependence_plot("LSTAT", shap_values, X_test)

DeepExplainer

DeepExplainer uses DeepLIFT to approximate SHAP values for deep learning models (PyTorch, TensorFlow/Keras). It is faster than KernelExplainer but limited to neural networks.

Python — SHAP with PyTorch
import shap
import torch
import torch.nn as nn

# Assume `model` is a trained PyTorch model
# and `X_train`, `X_test` are tensors

# Use a background dataset (subset of training data)
background = X_train[:100]

# Create DeepExplainer
explainer = shap.DeepExplainer(model, background)
shap_values = explainer.shap_values(X_test[:50])

# Visualize
shap.summary_plot(shap_values, X_test[:50].numpy())

KernelExplainer

KernelExplainer is model-agnostic — it works with any model that has a predict function. It is slower but universally applicable.

Python — KernelExplainer for any model
import shap
from sklearn.svm import SVR

# Train any model
model = SVR(kernel='rbf').fit(X_train, y_train)

# KernelExplainer works with any predict function
explainer = shap.KernelExplainer(model.predict, shap.sample(X_train, 100))
shap_values = explainer.shap_values(X_test[:20])

# Bar plot - mean absolute SHAP value per feature
shap.plots.bar(shap.Explanation(
    values=shap_values,
    data=X_test[:20],
    feature_names=feature_names
))

Choosing the Right Explainer

ExplainerModel TypeSpeedExactness
TreeExplainerTree-based (XGBoost, RF, LightGBM)Very fastExact
DeepExplainerNeural networks (PyTorch, TF)FastApproximate
GradientExplainerNeural networksFastApproximate
KernelExplainerAny modelSlowApproximate
LinearExplainerLinear modelsVery fastExact
Performance tip: Always use the most specific explainer for your model type. TreeExplainer is orders of magnitude faster than KernelExplainer for tree models. Use shap.sample() to reduce the background dataset size for KernelExplainer.

SHAP Visualization Types

  • Summary plot: Shows feature importance and effect direction across all samples. Each dot is one prediction.
  • Force plot: Explains a single prediction showing which features push the prediction higher or lower.
  • Dependence plot: Shows how a single feature affects predictions, revealing non-linear relationships and interactions.
  • Bar plot: Simple mean absolute SHAP values for global importance ranking.
  • Waterfall plot: Step-by-step breakdown of a single prediction from the base value.
💡
SHAP values are additive: The sum of all SHAP values for a prediction equals the difference between the model's output and the expected (average) output. This makes them ideal for explaining exactly how much each feature contributes.