Model Explainability (XAI): SHAP Values, LIME, Permutation Importance & PDP
How to open black-box ensembles — why default Gini tree importance lies, how game-theoretic SHAP credits every feature, and local vs. global explanations
Model Explainability (XAI) tools — SHAP, LIME, Permutation Importance, and Partial Dependence Plots (PDP) — reveal WHICH features drove a model's global behavior and WHY it made a specific prediction for one individual.
Regulators (credit, healthcare, hiring) legally require clear reasons for adverse decisions, and explainability catches silent shortcut bugs (like a cancer model cheating off hospital watermarks!).
Explainability turns a black-box XGBoost model into a debuggable, regulator-ready product that stakeholders actually trust.
Inspecting top SHAP features routinely exposes data leakage and upstream feature bugs within 60 seconds.
SHAP beeswarm plots show BOTH feature magnitude AND direction (e.g., high debt pushes default risk UP while high tenure pushes it DOWN).
Classic question: 'Why is Scikit-Learn's default feature_importances_ (Gini impurity) biased, and how do Permutation Importance and SHAP solve it?'
In a SHAP explanation for a single house priced at $420,000 where the dataset average (base value) is $350,000, what MUST all individual feature SHAP values sum to?
- Distinguish Global explanations (overall feature ranking) from Local explanations (why Customer #402 was denied a loan).
- Explain why default Tree Gini/MDI importance is biased toward high-cardinality numeric columns — and why Permutation Importance fixes it.
- Read a SHAP waterfall plot: Base Value + Σ SHAP_j = exact model prediction.
- Compare SHAP (exact game-theoretic additivity, fast TreeSHAP) vs. LIME (fast local linear surrogate, higher variance).
There are two questions stakeholders ask: (1) Global: 'Which features matter most overall?' (use Permutation Importance or mean ), and (2) Local: 'Why did the model reject THIS specific customer?' (use a SHAP Waterfall or LIME).
1. Comparing the 4 Core Explainability Tools
| Tool | Scope | Speed | Key Advantage | Watch Out For |
|---|---|---|---|---|
| Gini / MDI Importance | Global | Instant | Built into trees | Biased toward high-cardinality continuous features! |
| Permutation Importance | Global | Fast | Honest validation drop; model-agnostic | Diluted when two features are strongly correlated |
| SHAP (TreeSHAP) | Both | Fast for trees | Exact game-theoretic additivity + direction | Slow KernelSHAP for non-tree black boxes |
| LIME | Local | Fast | Works on text/images/any model | Perturbation randomness makes explanations slightly unstable |
Toggle Applicant Features & Watch Waterfall Bars Sum to Exact Risk
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
No! SHAP explains what the model relied on, not real-world causality. If ice_cream_sales correlates with drowning in your dataset, SHAP will faithfully report that the model used ice cream — exposing a correlation, not a causal law.
Var(Average) = ρ·σ² + ((1 − ρ) / B)·σ² — Watch Why Decorrelation Matters!
Notice that adding more trees B NEVER causes Random Forests to overfit — it just asymptotically approaches the correlation floor ρ·σ². Lowering ρ via max_features="sqrt" is the ONLY way to push the floor down!
import numpy as np
# Permutation Feature Importance From Scratch on Validation Data
rng = np.random.default_rng(42)
X_val = rng.normal(size=(200, 3))
# True model depends heavily on col 0, mildly on col 1, ZERO on col 2
y_val = 4.0 * X_val[:, 0] - 1.5 * X_val[:, 1] + rng.normal(0, 0.3, 200)
w = np.array([3.95, -1.48, 0.05])
def mse(X): return float(np.mean((y_val - X @ w) ** 2))
base_mse = mse(X_val)
for j, name in enumerate(["income", "debt", "random_noise"]):
X_perm = X_val.copy()
X_perm[:, j] = rng.permutation(X_perm[:, j])
drop = mse(X_perm) - base_mse # higher MSE increase = more important!
print(f"{name:14s} | MSE increase when shuffled: +{drop:.3f}")
feature_importances_ as ground truth.