Model Interpretation: SHAP, LIME, PDP & More
After this lesson, you will be able to:
- Tell global explanations (how does the model behave overall?) apart from local explanations (why did it make THIS one prediction?), and pick the right tool for each situation
- Compute permutation importance, partial dependence plots (PDP), and ICE curves to expose which features matter and how the model uses them, and know when correlated features make these plots lie
- Apply SHAP values (the gold standard for production explanations, grounded in Shapley values from game theory) and LIME (local linear surrogates) to explain individual predictions, and read SHAP summary, dependence, and force plots fluently
- Avoid the four big interpretation traps: confusing explanation with causation, trusting Gini importance instead of permutation importance, reading PDPs literally under feature correlation, and assuming an 'explainable' model is automatically fair
Before you start
Don't worry if "explaining a model" feels fuzzier than training one — there's a clean theory underneath, and the tools are mostly two-line library calls once you know which to reach for.
#War Story: The 95%-Accurate Model the VP Killed in a Meeting
"If a customer calls me to ask why their claim was flagged, what do I tell them?"
She said the model used 200 features and gradient-boosted trees of depth 8 — it was hard to summarize.
"Then we're not shipping it."
This isn't a soft-skills story. Regulation is moving the same way: rules such as the EU AI Act, US credit rules on adverse-action notices, and medical-device guidance expect meaningful explanations for high-stakes decisions. Details differ by jurisdiction and change over time (as of October 2026, verify the current rules for your case). In production ML, an unexplainable model is a non-starter regardless of its score.
pip install shap and 10 lines of code away. Let's see how.#Why Open the Black Box?
There are four reasons a model needs interpretation, and you will hit all of them in any real project:
- Debugging. Did the model learn the actual signal, or did it learn a leakage shortcut, a label artifact, or a spurious correlation? Interpretation is your inspection tool.
- Trust. Stakeholders (and you) need a reason to ship a model. "Accuracy 0.94" is not enough; "the top three drivers are X, Y, Z and they make sense" is.
- Compliance. GDPR's right to explanation, the EU AI Act, the US Equal Credit Opportunity Act's adverse action notices, FDA software-as-medical-device rules — all require model rationale.
- Improvement. Once you can see what the model relies on, you can engineer better features, fix biases, and remove brittleness.
#Global vs. Local: Two Different Questions
Interpretation methods answer one of two distinct questions, and confusing them is the #1 mistake beginners make.
| Question | Tool |
|---|---|
| Which features matter overall? | Permutation importance, SHAP summary plot |
| How does a feature affect predictions on average? | PDP, ALE |
| How does the effect of a feature vary across examples? | ICE plot, SHAP dependence plot |
| Why was THIS example classified the way it was? | LIME, SHAP force plot |
| What minimum change would flip the prediction? | Counterfactual explanation |
#Permutation Importance: The Honest Default
The formula is a recipe, so what number does it produce? For a linear model there is an exact answer, and it shows what permutation importance really measures. Open the derivation to see it.
#Partial Dependence and ICE: How a Feature Bends the Prediction
The average over rows is the whole recipe, so it helps to ask what the curve recovers when the answer is known. Open the derivation to see that, for a model that adds a part depending on x_S to a part depending on the rest, the PDP returns exactly the first part.
If the model contains an interaction, the average hides it. For a prediction equal to x_S times x_C, the PDP is x_S times the average of x_C, one straight line, while the ICE line of row i has slope equal to that row's own x_C. That fan of ICE lines is the picture the next box describes.
#When PDPs Lie: Feature Correlation
X_C using the marginal distribution of those features, which means it generates feature combinations that may never exist in reality. If age and years_employed are tightly correlated, the PDP for age evaluates the model at age=20, years_employed=40 — a row that does not exist in the real world — and includes those impossible rows in the average. The result can be misleading.Your model uses age and salary, which have correlation 0.7. The PDP for age shows a strange dip at age 25 that contradicts domain knowledge. What is most likely happening?
A feature has zero permutation importance on the validation set. Which interpretation is most defensible?
#SHAP: The Theory That Won
- Efficiency: the SHAP values for one prediction sum to (model output) − (model's expected output).
- Symmetry: two features that contribute identically to every coalition get the same SHAP value.
- Dummy: a feature that never changes the prediction has SHAP value zero.
- Additivity: SHAP values for an ensemble of two models equal the sum of the SHAP values for each.
These four axioms uniquely determine the formula:
The axioms say what a fair attribution must satisfy. The formula is easiest to see from a concrete picture: add the features to the prediction one at a time, in some order, and credit each feature with the change it causes. Open the first derivation to see that averaging this over all orders gives the formula with its odd-looking weights, and the second to see why the values add up to the prediction gap. That the four axioms leave no other rule is Shapley's theorem, which is stated here and not re-proved.
Here is the three-feature game worked out in full. Each row is one order of arrival, and each cell is the gain of that feature when it arrives. Every row adds to 0.57.
| Order | Gain of A | Gain of B | Gain of C |
|---|---|---|---|
| A, B, C | 0.24 | 0.20 | 0.13 |
| A, C, B | 0.24 | 0.28 | 0.05 |
| B, A, C | 0.35 | 0.09 | 0.13 |
| B, C, A | 0.38 | 0.09 | 0.10 |
| C, A, B | 0.25 | 0.28 | 0.04 |
| C, B, A | 0.38 | 0.15 | 0.04 |
| Average | 0.3067 | 0.1817 | 0.0817 |
Feature A is credited 0.31, B 0.18 and C 0.08. The three add to 0.57, the gap between the prediction 0.78 and the baseline 0.21.
Move the eight coalition values and the highlighted feature, and watch that a feature's Shapley value is its marginal gain averaged over all six arrival orders, that the three values add up exactly to the prediction minus the baseline, and that the subset-weight formula gives the same number.
In raw form the formula has 2^(n−1) terms — exponential in features, infeasible. The breakthroughs:
- TreeSHAP (Lundberg et al. 2020): for tree ensembles (XGBoost, LightGBM, CatBoost, Random Forests), the Shapley values can be computed in exact polynomial time O(L · D² · T) where L is leaves, D is depth, T is trees. This is what made SHAP a production-viable tool.
- DeepSHAP: a fast approximation for neural networks based on DeepLIFT.
- KernelSHAP: a slow model-agnostic fallback (uses LIME-style local sampling). Use only when a model-specific algorithm is unavailable.
#Concrete example: SHAP values on a single prediction
0.78 - 0.21 = +0.57 to individual features:| Feature | Value | SHAP value | What it means |
|---|---|---|---|
debt_to_income | 0.62 | +0.31 | High DTI strongly pushed prediction toward default |
credit_history_months | 8 | +0.18 | Short history pushed toward default |
late_payments_12m | 4 | +0.12 | Recent late payments contributed |
annual_income | $94,000 | -0.05 | Higher income pushed slightly away from default |
employment_years | 6 | +0.01 | Negligible effect |
| Sum | +0.57 | Equals (prediction - baseline), by the efficiency axiom |
This single table is what gets attached to an adverse-action letter. The applicant reads: "Your application was declined primarily due to (1) debt-to-income ratio above 50%, (2) limited credit history, and (3) recent late payments." Each reason is a real, attributable SHAP value, not a hand-waved explanation.
#The Three SHAP Plots You Will Actually Use
- Summary (beeswarm) plot. Each dot = one prediction. Y-axis = features sorted by mean(|SHAP|). X-axis = SHAP value. Color = feature value (red high, blue low). One plot tells you which features dominate, in which direction, and how much heterogeneity there is across rows.
- Dependence plot. SHAP value for one feature (Y) against that feature's raw value (X), one dot per row, colored by an interacting feature. Shows the nonlinear effect AND interactions.
- Force plot (or waterfall plot). For one prediction, shows how each feature pushes the output up or down from the baseline. This is the artifact you attach to an adverse-action letter.
Tests: Verify that informative features (0, 2, 4) rise to the top of permutation importance. Verify that Gini importance gives a different ranking than PI. Verify the PDP shows a roughly monotonic effect.
Permutation importance and SHAP both rank features, but they rank them differently — PI asks "what does shuffling hurt?", SHAP asks "what fraction of the prediction does this feature own under all coalition orderings?". The two rankings can legitimately disagree. Let's train a tiny Random Forest and see exactly how.
#LIME: Local Linear Surrogates
LIME's intuition is that any model is approximately linear in a small enough neighborhood. So: pick a row, perturb it many times, predict each perturbation with the real model, fit a sparse linear model to those (perturbation, prediction) pairs, and read the linear model's coefficients as the explanation.
The abstract objective above becomes concrete once you choose the loss and the family of g. With a squared loss weighted by closeness, a linear g and a ridge penalty, the fit has a closed form. Open the derivation to follow it.
LIME and SHAP give noticeably different top-3 features for the same denied loan applicant. Which is the more defensible default move for an adverse-action letter?
#Counterfactual Explanations: The Minimum-Change Rule
A counterfactual answers: "What is the smallest change to this input that would flip the model's decision?" For a denied loan applicant, the counterfactual might be: "If your debt-to-income ratio were 0.34 instead of 0.41, you would be approved."
A regulator asks 'Which features does this credit model use most heavily, in general?'. Which interpretability tool answers that question correctly?
#When To Use Which
| Situation | First reach for |
|---|---|
| You need ONE chart to summarize a tabular model | SHAP summary plot |
| You need to explain one specific prediction in production | SHAP force/waterfall plot |
| Adverse-action letter or actionable user feedback | Counterfactual + SHAP |
| Text classifier, individual prediction | LIME |
| Image classifier, single prediction | LIME or Grad-CAM |
| Honest "average effect" of one feature | ALE (or PDP if features are independent) |
| Quick model audit, no library available | Permutation importance |
#Key Takeaways
- Global vs. local is the first decision. Permutation importance and PDP/ALE answer "what matters on average?"; SHAP and LIME answer "why this prediction?". SHAP works at both levels and is the modern default.
- Permutation importance beats Gini. Gini importance is biased toward high-cardinality features and computed on training data; permutation importance is model-agnostic, validation-set-based, and tells you whether shuffling the feature actually hurts performance. Always swap.
- PDPs lie under feature correlation. Partial dependence averages the model over feature combinations that may not exist in the data. ALE plots fix this by working in local conditional bands. ICE alongside PDP reveals heterogeneity the average curve hides.
- SHAP is the unique fair attribution. Among all additive attribution methods, only Shapley values satisfy efficiency, symmetry, dummy, and additivity simultaneously. TreeSHAP makes this practical for tree ensembles in O(L·D²) per prediction.
- Explanation is not causation, and explainable is not fair. SHAP attributes correlations, not effects; an interpretable model can still encode systemic bias through proxy features. Pair interpretation with causal reasoning and fairness metrics, especially in regulated domains.
#Quick Check
Why is permutation importance generally preferred over the default `feature_importances_` from a Random Forest?
#The Bridge Forward: From Explanation to Deployment
- Model Evaluation told you the model is accurate enough to be useful.
- Probability Calibration ensures the model's confidence is honest — a prerequisite for SHAP values that make sense.
- Bias-Variance & Learning CurvesBias-Variance TradeoffThe bias-variance tradeoff describes how decreasing a model's bias (underfitting) typically increases its variance (overfitting), and vice versa.Learn more told you the model is on the right side of the U-curve — an overfit model's SHAP values explain noise, not signal.
- RegularizationRegularizationRegularization adds a penalty term to the loss function (L1, L2) to discourage overly complex models and reduce overfitting.Learn more and Hyperparameter Tuning got the model into the sweet spot before you tried to interpret it.