Explainable AI: SHAP, LIME, and Feature Attribution
Module: Machine Learning | Difficulty: Advanced
Shapley Values
SHAP (KernelSHAP)
LIME
Anchors
import numpy as np
from itertools import combinations
def shapley_values(model, X, background, n_samples=100):
n_features = X.shape[1]
phi = np.zeros(n_features)
for i in range(n_features):
for _ in range(n_samples):
perm = np.random.permutation(n_features)
pos = np.where(perm == i)[0][0]
before = perm[:pos]
after = perm[pos+1:]
# Marginal contribution
f_before = model(np.mean(background[:, before], axis=0, keepdims=True))
f_before_plus = model(np.mean(background[:, before + [i]], axis=0, keepdims=True))
phi[i] += (f_before_plus - f_before) / n_samples
return phi
Research Insight: SHAP values are the only feature attribution method that satisfies efficiency, symmetry, dummy, and linearity axioms. The computational complexity is exponential in the number of features, making exact computation intractable for large models.