scientific-skills/shap/references/explainers.md
This document provides comprehensive information about all SHAP explainer classes, their parameters, methods, and when to use each type.
SHAP provides specialized explainers for different model types, each optimized for specific architectures. The general shap.Explainer class automatically selects the appropriate algorithm based on the model type.
Purpose: Automatically uses Shapley values to explain any machine learning model or Python function by selecting the most appropriate explainer algorithm.
Constructor Parameters:
model: The model to explain (function or model object)masker: Background data or masker object for feature manipulationalgorithm: Optional override to force specific explainer typeoutput_names: Names for model outputsfeature_names: Names for input featuresWhen to Use: Default choice when unsure which explainer to use; automatically selects the best algorithm based on model type.
Purpose: Fast and exact SHAP value computation for tree-based ensemble models using the Tree SHAP algorithm.
Constructor Parameters:
model: Tree-based model (XGBoost, LightGBM, CatBoost, PySpark, or scikit-learn trees)data: Background dataset for feature integration (optional with tree_path_dependent)feature_perturbation: How to handle dependent features
"interventional": Requires background data; follows causal inference rules"tree_path_dependent": No background data needed; uses training examples per leaf"auto": Defaults to interventional if data provided, otherwise tree_path_dependentmodel_output: What model output to explain
"raw": Standard model output (default)"probability": Probability-transformed output"log_loss": Natural log of loss function"predict_proba"feature_names: Optional feature namingSupported Models:
Key Methods:
shap_values(X): Computes SHAP values for samples; returns arrays where each row represents feature attributionshap_interaction_values(X): Estimates interaction effects between feature pairs; provides matrices with main effects and pairwise interactionsexplain_row(row): Explains individual rows with detailed attribution informationWhen to Use:
Example:
import shap
import xgboost
# Train model
model = xgboost.XGBClassifier().fit(X_train, y_train)
# Create explainer
explainer = shap.TreeExplainer(model)
# Compute SHAP values
shap_values = explainer.shap_values(X_test)
# Compute interaction values
shap_interaction = explainer.shap_interaction_values(X_test)
Purpose: Approximates SHAP values for deep learning models using an enhanced version of the DeepLIFT algorithm.
Constructor Parameters:
model: Framework-dependent specification
nn.Module object or tuple of (model, layer) for layer-specific explanationsdata: Background dataset for feature integration
session (TensorFlow only): Optional session object; auto-detected if Nonelearning_phase_flags: Custom learning phase tensors for handling batch norm/dropout during inferenceSupported Frameworks:
Key Methods:
shap_values(X): Returns approximate SHAP values for the model applied to data Xexplain_row(row): Explains single rows with attribution values and expected outputssave(file) / load(file): Serialization support for explainer objectssupports_model_with_masker(model, masker): Compatibility checker for model typesWhen to Use:
Key Design Feature: Variance of expectation estimates scales approximately as 1/√N, where N is the number of background samples, enabling accuracy-efficiency trade-offs.
Example:
import shap
import tensorflow as tf
# Assume model is a Keras model
model = tf.keras.models.load_model('my_model.h5')
# Select background samples (subset of training data)
background = X_train[:100]
# Create explainer
explainer = shap.DeepExplainer(model, background)
# Compute SHAP values
shap_values = explainer.shap_values(X_test[:10])
Purpose: Model-agnostic SHAP value computation using the Kernel SHAP method with weighted linear regression.
Constructor Parameters:
model: Function or model object that takes a matrix of samples and returns model outputsdata: Background dataset (numpy array, pandas DataFrame, or sparse matrix) used to simulate missing featuresfeature_names: Optional list of feature names; automatically derived from DataFrame column names if availablelink: Connection function between feature importance and model output
"identity": Direct relationship (default)"logit": For probability outputsKey Methods:
shap_values(X, **kwargs): Calculates SHAP values for sample predictions
nsamples: Evaluation count per prediction ("auto" or integer); higher values reduce variancel1_reg: Feature selection regularization ("num_features(int)", "aic", "bic", or float)explain_row(row): Explains individual predictions with attribution values and expected valuessave(file) / load(file): Persist and restore explainer objectsWhen to Use:
Example:
import shap
from sklearn.svm import SVC
# Train model
model = SVC(probability=True).fit(X_train, y_train)
# Create prediction function
predict_fn = lambda x: model.predict_proba(x)[:, 1]
# Select background samples
background = shap.sample(X_train, 100)
# Create explainer
explainer = shap.KernelExplainer(predict_fn, background)
# Compute SHAP values (may be slow)
shap_values = explainer.shap_values(X_test[:10])
Purpose: Specialized explainer for linear models that accounts for feature correlations.
Constructor Parameters:
model: Linear model or tuple of (coefficients, intercept)masker: Background data for feature correlationfeature_perturbation: How to handle feature correlations
"interventional": Assumes feature independence"correlation_dependent": Accounts for feature correlationsSupported Models:
When to Use:
Example:
import shap
from sklearn.linear_model import LogisticRegression
# Train model
model = LogisticRegression().fit(X_train, y_train)
# Create explainer
explainer = shap.LinearExplainer(model, X_train)
# Compute SHAP values
shap_values = explainer.shap_values(X_test)
Purpose: Uses expected gradients to approximate SHAP values for neural networks.
Constructor Parameters:
model: Deep learning model (TensorFlow or PyTorch)data: Background samples for integrationbatch_size: Batch size for gradient computationlocal_smoothing: Amount of noise to add for smoothing (default 0)When to Use:
Example:
import shap
import torch
# Assume model is a PyTorch model
model = torch.load('model.pt')
# Select background samples
background = X_train[:100]
# Create explainer
explainer = shap.GradientExplainer(model, background)
# Compute SHAP values
shap_values = explainer.shap_values(X_test[:10])
Purpose: Approximates Shapley values by iterating through permutations of inputs.
Constructor Parameters:
model: Prediction functionmasker: Background data or masker objectmax_evals: Maximum number of model evaluations per sampleWhen to Use:
Example:
import shap
# Create explainer
explainer = shap.PermutationExplainer(model.predict, X_train)
# Compute SHAP values
shap_values = explainer.shap_values(X_test[:10])
Decision Tree for Choosing an Explainer:
Is your model tree-based? (XGBoost, LightGBM, CatBoost, Random Forest, etc.)
TreeExplainer (fast and exact)Is your model a deep neural network? (TensorFlow, PyTorch, Keras)
DeepExplainer or GradientExplainerIs your model linear? (Linear/Logistic Regression, GLMs)
LinearExplainer (extremely fast)Do you need model-agnostic explanations?
KernelExplainer (slower but works with any model)PermutationExplainerUnsure or want automatic selection?
shap.Explainer (auto-selects best algorithm)Background Data / Masker:
Feature Names:
Model Output Specification:
Speed Ranking (fastest to slowest):
LinearExplainer - Nearly instantaneousTreeExplainer - Very fast, scales wellDeepExplainer - Fast for neural networksGradientExplainer - Fast for neural networksKernelExplainer - Slow, use only when necessaryPermutationExplainer - Very slow but most accurate for small feature setsMemory Considerations:
TreeExplainer: Low memory overheadDeepExplainer: Memory proportional to background sample sizeKernelExplainer: Can be memory-intensive for large background datasetsAll explainers return shap.Explanation objects containing:
values: SHAP values (numpy array)base_values: Expected model output (baseline)data: Original feature valuesfeature_names: Names of featuresThe Explanation object supports:
explanation[0] for first sample