Contents/ Part XVIII · Supervised Learning/ Chapter 117

Model Interpretability & Explainability

A model that predicts well but cannot say why is a liability, unauditable, undebuggable, and often illegal. This chapter opens the black box: permutation importance and partial dependence for the global picture, and SHAP and LIME to justify a single decision, so you can answer not just "what does the model predict?" but "why?"

⏱️ ~20 min read
🐍 Notebook included
📊 Chapter 117

The most accurate models, gradient-boosted forests, deep networks, are the least transparent: hundreds of trees or millions of weights offer no readable rule. Yet a bank that denies a loan, a hospital that flags a patient, or a court that scores a defendant must be able to explain the decision. Interpretability is how we get accuracy and accountability.

?
Interpretability methods explain a model's behavior. Global methods (permutation importance, partial dependence) describe the model overall; local methods (SHAP, LIME) explain a single prediction as a set of per-feature contributions.
🔍
The chapter in one line

Explain a model two ways: globally (which features matter, and how, via permutation importance and partial dependence) and locally (why this one prediction, via SHAP and LIME), because trust, debugging, fairness, and the law all demand a reason, not just a number.

1

Why Interpretability, and Two Kinds

An unexplained model cannot be trusted, debugged, or defended. Interpretability serves four concrete needs: trust (would you accept a diagnosis with no reasoning?), debugging (is the model right for the wrong reason, leaning on a leaked or biased feature?), fairness (does it discriminate?), and regulation (laws increasingly grant a right to an explanation).

Explanations come at two scales, and you need both.

Two scales of explanation GLOBAL · the whole model which features matter, and how importance: debt-to-income credit score late payments income partial dependence: risk falls as credit score rises LOCAL · one prediction why was THIS applicant denied? base rate 0.32 + high DTI + big loan −emp 0.86 each feature pushes the prediction up (red) or down (blue) until it reaches this applicant's score this is SHAP / LIME

Global explanations describe how the model behaves across all data, its overall priorities and learned relationships. Local explanations justify one specific prediction. A loan applicant does not care which features matter on average; they want to know why they were denied. We build both.

2

Global: Importance & Partial Dependence

Two global tools answer two questions. Permutation importance answers which features matter: it shuffles one feature's values, breaking its link to the target, and measures how far the model's accuracy falls. A big drop means the model relies on it heavily. It is model-agnostic and more trustworthy than a tree's built-in impurity importances. On the credit model, debt-to-income, credit score, and late payments lead, precisely what a human analyst would expect.

Partial dependence answers how a feature acts: it sweeps one feature across its range and plots the model's average predicted probability. In the notebook, predicted default falls as credit score rises and climbs with debt-to-income, non-linear curves whose steepest regions reveal the model's decision thresholds. (Its per-instance cousin, the ICE plot, draws one line per applicant to check whether the effect is the same for everyone.) Together they give the model's overall logic; but they cannot explain a single denial. For that we go local.

3

Local: SHAP & LIME

SHAP (SHapley Additive exPlanations) borrows a fair-credit-assignment idea from game theory: it splits each prediction into an additive contribution from every feature, in a mathematically consistent way. A single applicant's prediction becomes a running total, start at the average, then add each feature's push, up toward default or down toward repayment, until you reach their score.

Why this applicant was flagged: a SHAP waterfall 0.01.0 base rate 0.32 loan amount high +0.25 debt-to-income high +0.19 credit score low +0.16 employed 9 yrs −0.06 prediction 0.86 default red pushes toward default, blue toward repayment; the pushes sum to the final score

The waterfall above is the artifact you hand a customer or an auditor: not a bare denial, but a ranked, quantitative reason, a large loan and high debt-to-income did most of the damage, only slightly offset by a steady employment history. LIME (Local Interpretable Model-agnostic Explanations) reaches the same goal differently: it probes the black box with small perturbations around one applicant and fits a simple linear model to that neighborhood. When SHAP and LIME surface the same drivers, as they do here, you can trust the explanation.

4

Real-World Example: Explaining a Credit Decision

A lender trains a gradient-boosting model on past applicants and, by law, must give any rejected applicant the reasons. Interpretability turns an opaque score into a defensible decision.

📂 Dataset · model-interpretability-and-explainability--credit.xlsx

One row per applicant with credit_score, debt_to_income, income_k, loan_amount_k, num_late_payments, and employment_years, plus the label default.

QuestionToolAnswer on the credit model
Which features matter overall?permutation importancedebt-to-income, credit score, late payments
How does a feature act?partial dependencerisk falls with credit score, rises with debt-to-income
Why was this applicant flagged (0.91)?SHAP waterfalla large loan and high debt-to-income drove the risk
A second opinion on that applicant?LIMEagrees on the same key drivers

The workflow is the point: global first to sanity-check that the model learned sensible relationships (and is not leaning on a biased or leaked feature), then local to generate the specific, ranked reason codes each decision requires. That is how a black-box model becomes something you can trust, debug, and defend in front of a regulator.

5

Interpretability in Machine Learning & AI

Explainable AI (XAI) has grown from a nice-to-have into a requirement, technically and legally.

Tool (this chapter)Where it is used
Permutation importancemodel debugging, feature engineering, leak detection
Partial dependence / ICEvalidating that learned relationships make sense
SHAPthe industry-standard explainer; regulatory reason codes
LIMEmodel-agnostic local explanations for tabular, text, and images
Interpretability broadlyfairness audits, the legal "right to explanation"
🤖
Why this matters for AI research

The more powerful the model, the more it needs explaining, and the harder it gets. Regulations such as the GDPR's right to explanation and fair-lending laws already require that automated decisions be justifiable, so SHAP-style reason codes are now standard in banking and insurance. At the frontier, an entire field of mechanistic interpretability tries to reverse-engineer what individual neurons and attention heads inside large language models actually compute, work that is central to AI safety, because we cannot align or trust a system we do not understand. The gap between a model's capability and our ability to explain it is one of the defining problems of modern AI, and the tools in this chapter are where learning to close it begins.

🐍

Explain a model in Python

The companion notebook trains a credit-risk model, then explains it with permutation importance, partial dependence plots, a SHAP summary (beeswarm) and a per-applicant SHAP waterfall, and a LIME local explanation, nothing left as a black box.

📓 View Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub

View opens the rendered notebook instantly. Open in Colab runs it live. To run locally, install numpy, pandas, scikit-learn, shap, lime, matplotlib, and openpyxl.

🎓 Key Takeaways

  • Interpretability is not optional: it underpins trust, debugging, fairness, and legal compliance for deployed models.
  • Global explanations describe the whole model: permutation importance (which features matter) and partial dependence (how they act).
  • Permutation importance is model-agnostic and more reliable than a tree's built-in impurity importances.
  • SHAP decomposes any single prediction into additive, consistent per-feature contributions, a beeswarm globally, a waterfall for one decision.
  • LIME explains one prediction by fitting a simple linear model in its local neighborhood; when SHAP and LIME agree, trust the reason.
6

Practice Challenges

Five short challenges. Try them with scikit-learn, shap, and lime before checking the solutions.

1

Permutation importance

Rank the features by permutation importance and name the most important.

Hint: permutation_importance(model, X, y).
2

Partial dependence direction

Show the model's predicted risk falls as credit score rises.

Hint: partial_dependence(model, X, ["credit_score"]); cast to float.
3

Global SHAP importance

Rank the features by mean absolute SHAP value.

Hint: np.abs(shap_values.values).mean(axis=0).
4

SHAP for one applicant

For the highest-risk applicant, find the feature contributing most to the default prediction.

Hint: the largest positive SHAP value.
5

LIME agreement

Explain the same applicant with LIME and compare its top driver to SHAP.

Hint: LimeTabularExplainer(...).explain_instance(...).
Check your work

A fully-worked solutions notebook walks through all five challenges, each verified in code. Try them yourself first, then compare.

📓 View Solutions ▶ Open Solutions in Colab ⬇ View / Download on GitHub
7

Quiz: Test Yourself

Eight quick questions on interpretability. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.

➡️
Up next: a new kind of learning

That completes supervised learning, every chapter so far has had a labeled target. Next we drop the labels. Clustering opens Unsupervised Learning, where the goal is to find structure, groups, patterns, and anomalies, with no answer key at all.