flowchart TD
subgraph Data["Training Cohort"]
D0["Clean Features X, Target y<br/>(Stratified 75/25 Split)"]
end
subgraph Bag["Bagging: Parallel"]
B1["Bootstrap 1 -> Tree 1<br/>(max_features=sqrt)"]
B2["Bootstrap 2 -> Tree 2<br/>(OOB Rows Held Out)"]
B3["Average Probabilities<br/>Low Variance + OOB Score"]
end
subgraph Boost["Boosting: Serial"]
G1["Init F0 -> Shallow Tree 1<br/>Fits Residuals r1 = y - F0"]
G2["Update F1 = F0 + lr * T1<br/>Tree 2 Fits Residuals r2"]
G3["Additive Ensemble F_M<br/>Low Bias + Native NaNs"]
end
subgraph Explain["Interpretability"]
E1["Global: Permutation AUC<br/>(Held-Out Test Set)"]
E2["Local: Shapley Values<br/>phi_0 + sum(phi_i) = f(x)"]
end
D0 --> B1
D0 --> B2
B1 --> B3
B2 --> B3
D0 --> G1
G1 --> G2
G2 --> G3
B3 --> E1
G3 --> E1
E1 --> E2
24 Tree-Based Models, Ensembles, and Interpretability — Decision Trees, Random Forests, Gradient Boosting, and SHAP Intuition
Chapter Mission: Unlock the workhorse model family of modern tabular data science and clinical prediction. Using Bangladesh maternal and child health survey data (
mch_survey_sample) alongside Scikit-Learn’sDecisionTreeClassifier,RandomForestClassifier,HistGradientBoostingClassifier, andpermutation_importance, you will build impurity criteria and greedy decision stumps from scratch, tame overfitting with Bagging and Out-of-Bag (OOB) tuning, understand sequential residual Gradient Boosting (and howHistGradientBoosting, XGBoost, and LightGBM relate), expose default feature-importance bias, and code exact Shapley attributions from first principles.
Chapter Companion Notebooks (1-to-1 Mirrored in Google Colab)
| Lab Type | Interactive Colab Notebook | Scan (Print) |
|---|---|---|
| Walkthrough | ch24 / 01_walkthrough.ipynb | ![]() |
| Guided To-Dos | ch24 / 02_guided_todos.ipynb | ![]() |
| Coding Exam | ch24 / 03_coding_exam.ipynb | ![]() |
Learning Objectives
By the end of this chapter, you will be able to:
- Compute Gini impurity (\(1 - \sum p_k^2\)), base-2 Shannon entropy (\(-\sum p_k \log_2 p_k\)), and greedy CART decision stump midpoint splits from scratch in pure NumPy.
- Diagnose the bias–variance trade-off of single Decision Trees by sweeping
max_depthand measuring the generalization gap (train_auc - val_auc). - Contrast Bagging (
RandomForestClassifierwith parallel bootstrap trees and Out-of-Bag (OOB) validation) against Gradient Boosting (HistGradientBoostingClassifierfitting sequential residuals, plus its conceptual relationship to XGBoost and LightGBM). - Explain why
HistGradientBoostingClassifierhandles missing (NaN) clinical measurements natively while classicGradientBoostingClassifierraisesValueError. - Unmask the high-cardinality bias of default training-set Mean Decrease in Impurity (MDI) by contrasting
.feature_importances_against held-out Permutation Importance (sklearn.inspection.permutation_importance). - Implement exact 3-feature Shapley value attribution across all \(3! = 6\) permutations, verify the Efficiency Axiom (\(\phi_0 + \sum_i \phi_i = f(x)\)), and interpret local SHAP explanations for clinical risk prediction.
24.1 Stage 1 — First-Principles Intuition: Why Trees & Ensembles Rule Tabular Science
Linear and logistic regression assume that every predictor shifts the log-odds smoothly along a straight line. Real clinical and epidemiological data rarely obey neat straight lines:
- Threshold Effects: Maternal hemoglobin below \(11.0\text{ g/dL}\) signals clinical anemia, and fewer than \(4\) antenatal care (ANC) visits marks WHO-defined inadequate care. A Decision Tree discovers these exact cutoffs automatically.
- Automatic Feature Interactions: Low wealth paired with fewer than \(4\) ANC visits may elevate child stunting risk far beyond the sum of either factor alone. Every root-to-leaf path in a tree is a natural interaction rule (
wealth_rank <= 2.5 AND anc_visits <= 3.5). - Monotonic Scale Invariance: Because trees split on order thresholds (\(x \le t\)), you do not need
StandardScaleror log-transforms for tree splits to work.
Yet a single unconstrained Decision Tree has an Achilles’ heel: extreme variance. Grow a tree deep enough and it creates one tiny leaf per training household—scoring near 1.000 training AUC while collapsing on new patients.
Modern machine learning solves single-tree fragility through two complementary ensemble philosophies:
- Bagging & Random Forests (Variance Reduction in Parallel): Train \(B\) deep trees independently on bootstrap resamples of the training rows, randomly restricting each split to a subset of features (
max_features="sqrt"). Averaging decorrelated trees slashes variance dramatically, and the ~36.8% of rows omitted from each bootstrap sample provide near-free Out-of-Bag (OOB) validation! - Gradient Boosting (Bias Reduction in Sequence): Train shallow trees (“weak learners”) one after another, where each new tree predicts the residual error left behind by the ensemble so far, scaled by a shrinkage learning rate \(\eta\).
- Histogram Boosting, XGBoost & LightGBM: Modern engines such as XGBoost, LightGBM, and Scikit-Learn’s
HistGradientBoostingClassifierbin continuous features into ~255 quantile histograms (making split search \(10\times\)–\(100\times\) faster), apply \(L_2\) leaf regularization, and learn optimal split directions for missing (NaN) values natively.
| Framework | Tree Growth | Native NaN? |
Core Strengths in Research |
|---|---|---|---|
Scikit-Learn HistGradientBoosting |
Leaf-wise (binned) | Yes | Zero extra installs; native Pipeline fit |
XGBoost (xgboost) |
Depth-wise (exact/hist) | Yes | \(L_1\)/\(L_2\) leaf penalties; GPU & sparse data |
LightGBM (lightgbm) |
Leaf-wise (GOSS/EFB) | Yes | Fastest on \(100\text{k}+\) rows; native categories |
Finally, predictive accuracy alone never earns clinical trust. Peer reviewers demand interpretability—both global (which predictors drive discrimination on held-out patients?) and local (why did the model assign this specific household a 25.7% stunting probability?). Held-out Permutation Importance answers the global question without MDI’s high-cardinality trap, while Shapley values (SHAP) answer the local question with game-theoretic fairness.
Always separate global from local explanations in your methods section:
- Global (Held-Out Permutation Importance): Shuffle one feature column at a time on the test set and measure how much ROC-AUC drops. Noise features collapse near zero.
- Local (Shapley / SHAP Values): Explain one patient’s prediction \(f(x)\) as a baseline risk \(\phi_0\) plus additive feature credits \(\phi_1 + \phi_2 + \dots + \phi_p = f(x) - \phi_0\), averaged across every possible ordering of features.
24.2 Stage 2 — Visual Mental Model: Bagging vs. Boosting & Interpretability
Compare how Bagging (Random Forests) averages independent parallel trees to cut variance, while Boosting (HistGradientBoosting) chains shallow residual trees sequentially to cut bias—and how both feed into held-out interpretability:
Notice the structural contrast at a glance: in Bagging, Tree 1 and Tree 2 never communicate—they train in parallel on different bootstrap draws so their errors cancel when averaged. In Boosting, Tree 2 cannot begin until Tree 1 finishes and exposes the remaining residuals \(r_1\).
- Default
.feature_importances_(MDI) Favors Noise: Impurity-based importance is computed on the training set and systematically inflates high-cardinality continuous features—even pure random IDs! Always audit with held-outpermutation_importance. - Unbounded Depth Memorizes Small Cohorts: A single tree with
max_depth=Nonecan hittrain_auc = 1.000while dropping below a simple stump on validation. Always inspecttrain_auc - val_auc. - Never Assume Boosting Automatically Wins on Small \(n\): On a \(500\)-row public-health survey, shallow regularized trees or logistic models can rival or beat un-tuned ensembles. Always benchmark all three families empirically on the same stratified split!
24.3 Stage 3 — Line-by-Line Code Walkthrough & Error Anatomy
Open part_05_classical_ml/ch24_trees_ensembles_interpretability/01_walkthrough.ipynb in Colab and execute every cell top-to-bottom alongside the dissections below.
24.3.1 Setup: Loading & Preparing mch_survey_sample
import itertools
import numpy as np
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.ensemble import (
GradientBoostingClassifier,
HistGradientBoostingClassifier,
RandomForestClassifier,
)
from sklearn.inspection import permutation_importance
from sklearn.metrics import average_precision_score, roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
import sharukh_tutor as tutor
mch_raw = tutor.load_data("mch_survey_sample", as_pandas=True)
mch = mch_raw.copy()
mch["residence"] = mch["residence"].astype(str).str.strip().str.title()
mch["anc_visits"] = mch["anc_visits"].replace(999, np.nan)
mch["is_urban"] = (mch["residence"] == "Urban").astype(int)
wealth_map = {
"Poorest": 1, "Poorer": 2, "Middle": 3, "Richer": 4, "Richest": 5
}
mch["wealth_rank"] = mch["wealth_quintile"].map(wealth_map)
features = [
"maternal_age_years", "anc_visits", "maternal_hb_g_dl",
"is_urban", "wealth_rank",
]
clean = mch[features + ["child_stunted"]].dropna()
X = clean[features]
y = clean["child_stunted"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, stratify=y, random_state=42
)
print("Clean complete-case shape:", clean.shape)
print(f"Train: {len(X_train)}, Test: {len(X_test)}, "
f"Stunting rate: {y.mean():.4f}")Expected Output:
Notice two crucial data-hygiene steps before fitting any tree:
- Sentinel replacement (
999 -> np.nan): If999stayed insideanc_visits, a tree would treat missingness as “999 clinic visits”—an extreme right-tail artifact. - Ordinal mapping (
wealth_rank1..5): Mapping"Poorest"through"Richest"to integers1..5preserves the socioeconomic ordering so a single split (wealth_rank <= 2.5) separates the bottom two quintiles from the upper three cleanly. - Stratified split (
stratify=y): Because child stunting prevalence is imbalanced (13.83%), stratifying preserves the exact same class ratio in both train (352rows) and test (118rows).
24.3.2 Gini Impurity, Shannon Entropy & Greedy Decision Stump
For a node with class proportions \(p_1, \dots, p_K\), CART measures node disorder using Gini impurity or base-2 Shannon entropy:
\[G(y) = 1 - \sum_{k=1}^{K} p_k^2, \qquad H(y) = -\sum_{k=1}^{K} p_k \log_2(p_k)\]
A decision stump tests every midpoint \(t\) between consecutive sorted unique values of feature \(x\) and picks the threshold minimizing weighted child Gini:
\[G_{\text{split}}(t) = \frac{n_L}{n} G(y_{x \le t}) + \frac{n_R}{n} G(y_{x > t})\]
def gini_demo(y_vals):
arr = np.asarray(y_vals)
if arr.size == 0:
return 0.0
_, counts = np.unique(arr, return_counts=True)
probs = counts / arr.size
return round(float(1.0 - np.sum(probs ** 2)), 4)
def entropy_demo(y_vals):
arr = np.asarray(y_vals)
if arr.size == 0:
return 0.0
_, counts = np.unique(arr, return_counts=True)
probs = counts / arr.size
return round(float(-np.sum(probs * np.log2(probs))), 4)
def stump_demo(x_vals, y_vals):
x = np.asarray(x_vals, dtype=float)
yv = np.asarray(y_vals)
uniq = np.unique(x)
mids = (uniq[:-1] + uniq[1:]) / 2.0
best_t, best_g = float(mids[0]), float("inf")
for t in mids:
left, right = yv[x <= t], yv[x > t]
wg = (len(left) / len(yv)) * gini_demo(left)
wg += (len(right) / len(yv)) * gini_demo(right)
if wg < best_g:
best_g, best_t = wg, float(t)
return (round(best_t, 4), round(best_g, 4))
print("Root Gini:", gini_demo(y_train),
"| Root Entropy:", entropy_demo(y_train))
for col in ["wealth_rank", "anc_visits", "maternal_hb_g_dl"]:
t, g = stump_demo(X_train[col], y_train)
print(f"Best stump on {col:16s}: thresh={t:.2f}, w_gini={g:.4f}")Expected Output:
Look at the epidemiology our pure-NumPy stump just discovered automatically:
wealth_rank <= 2.50(w_gini = 0.2171): Achieves the largest impurity drop (0.2397 -> 0.2171) by separatingPoorest/Poorerhouseholds fromMiddle/Richer/Richest.anc_visits <= 3.50(w_gini = 0.2259): Rediscovers the World Health Organization’s classic 4+ antenatal care visits benchmark (<= 3vs.>= 4) directly from data!
24.3.3 Decision Tree Overfitting & Depth Sweep
depth_rows = []
for d in [1, 2, 3, 5, 8, 12]:
dt = DecisionTreeClassifier(max_depth=d, random_state=42)
dt.fit(X_train, y_train)
tr_auc = roc_auc_score(y_train, dt.predict_proba(X_train)[:, 1])
te_auc = roc_auc_score(y_test, dt.predict_proba(X_test)[:, 1])
depth_rows.append({
"max_depth": d,
"train_auc": round(float(tr_auc), 4),
"val_auc": round(float(te_auc), 4),
"gap": round(float(tr_auc - te_auc), 4),
})
print(pd.DataFrame(depth_rows).to_string(index=False))Expected Output:
This table is the textbook signature of tree overfitting:
- From
max_depth = 1to5, validation ROC-AUC climbs from0.6213to0.6550. - Beyond depth
5, training AUC shoots to0.9993(nearly memorizing every single household!), while validation AUC plunges to0.5809—a massive generalization gap of0.4184!
24.3.4 Bagging, Random Forests & Out-of-Bag (OOB) Validation
When a Random Forest draws a bootstrap sample of size \(n\) with replacement, the probability that any given household is never selected in a single tree is \((1 - 1/n)^n \approx e^{-1} \approx \mathbf{0.368}\). Evaluating every household using only the ~36.8% of trees that never saw it produces the Out-of-Bag (OOB) score—free internal validation without touching the test set!
Expected Output:
OOB validation cleanly peaks at max_depth = 4 (0.8636) and declines as deeper trees memorize noise in the bootstrap draws. Remember: oob_score_ reports accuracy by default, so always confirm discrimination on imbalanced outcomes with ROC-AUC and PR-AUC.
24.3.5 Gradient Boosting Residuals, Native NaNs & Ensemble Scaling
In Gradient Boosting, each tree \(h_m(x)\) fits the pseudo-residuals left over by the previous ensemble \(F_{m-1}(x)\) and updates predictions cautiously via learning rate \(\eta\):
\[r_i^{(m)} = y_i - F_{m-1}(x_i), \qquad F_m(x) = F_{m-1}(x) + \eta \cdot h_m(x)\]
# 1. Sequential residual fitting intuition (3 boosting rounds)
y_hb = np.array([9.4, 10.1, 11.2, 12.6, 13.0])
x_anc = np.array([[0], [1], [3], [6], [8]])
pred = np.full_like(y_hb, y_hb.mean())
lr = 0.5
print(f"Step 0 (mean) MSE: {np.mean((y_hb - pred) ** 2):.4f}")
for step in range(1, 4):
resid = y_hb - pred
stump = DecisionTreeRegressor(max_depth=1, random_state=42)
stump.fit(x_anc, resid)
pred = pred + lr * stump.predict(x_anc)
print(f"Step {step} (boost) MSE: {np.mean((y_hb - pred) ** 2):.4f}")
# 2. Native NaN handling of HistGradientBoosting on all 500 MCH rows
X_full = mch[features]
y_full = mch["child_stunted"]
Xf_tr, Xf_te, yf_tr, yf_te = train_test_split(
X_full, y_full, test_size=0.25, stratify=y_full, random_state=42
)
hgb_nan = HistGradientBoostingClassifier(max_depth=4, random_state=42)
hgb_nan.fit(Xf_tr, yf_tr)
nan_auc = roc_auc_score(yf_te, hgb_nan.predict_proba(Xf_te)[:, 1])
n_nans = int(X_full.isna().sum().sum())
print(f"HGB native NaN (n={len(X_full)}, NaNs={n_nans}) "
f"test ROC-AUC: {nan_auc:.4f}")
# 3. Ensemble scaling on a 600-row nonlinear cohort
X_syn, y_syn = make_classification(
n_samples=600, n_features=6, n_informative=4, n_redundant=1,
weights=[0.80, 0.20], random_state=42,
)
Xs_tr, Xs_te, ys_tr, ys_te = train_test_split(
X_syn, y_syn, test_size=0.25, stratify=y_syn, random_state=42
)
specs = [
("DecisionTree", DecisionTreeClassifier(max_depth=5, random_state=42)),
("RandomForest", RandomForestClassifier(
n_estimators=100, max_depth=6, random_state=42)),
("HistGradientBoosting", HistGradientBoostingClassifier(
max_depth=4, random_state=42)),
]
for name, clf in specs:
clf.fit(Xs_tr, ys_tr)
p = clf.predict_proba(Xs_te)[:, 1]
print(f"Synth {name:20s} ROC={roc_auc_score(ys_te, p):.4f} "
f"PR={average_precision_score(ys_te, p):.4f}")Expected Output:
Three powerful lessons jump out of this output:
- Residual Shrinkage in Action: Just three depth-1 stumps shrink MSE from
1.9264down to0.1090by focusing every new stump strictly on the unexplained error of earlier rounds. - Native Missing-Value Splitting Wins: Instead of dropping 30 households with missing hemoglobin or ANC values,
HistGradientBoostingClassifiertrains directly on all 500 rows with30NaNs intact and leaps toROC-AUC = 0.7004! - Ensemble Dominance on Nonlinear Tabular Signal: On the 600-row nonlinear cohort,
HistGradientBoosting(ROC = 0.9246, PR = 0.8237) decisively beatsRandomForest(0.8932 / 0.7627), which in turn crushes a singleDecisionTree(0.8162 / 0.6082).
24.3.6 Interpretability: Permutation Importance vs. Default MDI Bias
Let’s test Scikit-Learn’s default .feature_importances_ (Mean Decrease in Impurity, MDI) by injecting a pure random noise column (random_noise_id) into mch_survey_sample:
rng = np.random.default_rng(42)
noisy = clean.copy()
noisy["random_noise_id"] = rng.uniform(0, 1000, size=len(noisy))
f_noise = features + ["random_noise_id"]
Xn_tr, Xn_te, yn_tr, yn_te = train_test_split(
noisy[f_noise], noisy["child_stunted"],
test_size=0.25, stratify=noisy["child_stunted"], random_state=42,
)
rf_noise = RandomForestClassifier(
n_estimators=100, max_depth=6, random_state=42
).fit(Xn_tr, yn_tr)
pi_noise = permutation_importance(
rf_noise, Xn_te, yn_te, scoring="roc_auc",
n_repeats=10, random_state=42,
)
imp_df = pd.DataFrame({
"feature": f_noise,
"mdi_impurity": np.round(rf_noise.feature_importances_, 4),
"perm_auc_mean": np.round(pi_noise.importances_mean, 4),
"perm_auc_std": np.round(pi_noise.importances_std, 4),
}).sort_values("mdi_impurity", ascending=False)
print(imp_df.to_string(index=False))Expected Output:
Pause and absorb how staggering that table is:
- Default MDI (
mdi_impurity) ranked pure random noise #1 (0.2316)—above genuine socioeconomic driverwealth_rank(0.1719)! Why? Because a continuous uniform column has 352 unique split candidates to slice training noise, whereaswealth_rankonly has 4 possible split points (1.5, 2.5, 3.5, 4.5). - Held-Out Permutation Importance (
perm_auc_mean) immediately unmasks the fraud: Shufflingwealth_rankon the test set slashes ROC-AUC by+0.1135, ranking it #1 by a landslide, whilerandom_noise_idcollapses to0.0140 ± 0.0185(indistinguishable from zero!).
24.3.7 Exact 3-Feature Shapley Values (SHAP Intuition) From Scratch
How do we explain one individual household’s predicted stunting probability \(f(x)\)? Cooperative game theory’s Shapley value treats each feature as a player joining a coalition from a reference baseline_row to x_row. Because tree models contain feature interactions, the marginal credit of revealing feature \(i\) depends on which features were already revealed. Averaging across all \(p!\) permutations yields the unique attribution satisfying Efficiency, Symmetry, Dummy, and Additivity:
\[\phi_i(x) = \frac{1}{p!} \sum_{\pi \in S_p} \Big[ f\big(S_\pi(i) \cup \{i\}\big) - f\big(S_\pi(i)\big) \Big], \qquad \phi_0 + \sum_{i=1}^{p} \phi_i(x) = f(x)\]
top3 = ["wealth_rank", "anc_visits", "maternal_hb_g_dl"]
rf3 = RandomForestClassifier(
n_estimators=100, max_depth=4, random_state=42
).fit(X_train[top3].to_numpy(), y_train.to_numpy())
def rf3_proba(v):
arr = np.asarray(v, dtype=float).reshape(1, -1)
return float(rf3.predict_proba(arr)[0, 1])
def shapley_3_demo(predict_fn, x_row, base_row):
x = np.asarray(x_row, dtype=float)
base = np.asarray(base_row, dtype=float)
base_val = float(predict_fn(base))
pred_val = float(predict_fn(x))
contribs = np.zeros(3, dtype=float)
perms = list(itertools.permutations([0, 1, 2]))
for perm in perms:
state = base.copy()
prev = base_val
for idx in perm:
state[idx] = x[idx]
curr = float(predict_fn(state))
contribs[idx] += curr - prev
prev = curr
shap_vals = contribs / len(perms)
return {
"base_value": round(base_val, 4),
"shap_values": [round(float(v), 4) for v in shap_vals],
"prediction": round(pred_val, 4),
}
baseline = X_train[top3].median().to_numpy()
high_risk = np.array([1.0, 1.0, 9.5])
sh = shapley_3_demo(rf3_proba, high_risk, baseline)
print("Baseline (median):", np.round(baseline, 2).tolist())
print("High-risk patient:", high_risk.tolist())
print("Base:", sh["base_value"], "| Pred:", sh["prediction"])
print("Shapley values:", sh["shap_values"])
eff = round(sh["base_value"] + sum(sh["shap_values"]), 4)
print(f"Efficiency check: {eff} ~= {sh['prediction']}")Expected Output:
Read this patient-level waterfall attribution like a clinician:
- Starting from the median household baseline (
wealth_rank = 3,anc_visits = 4,Hb = 11.5 g/dL), predicted stunting risk is3.65%(base_value = 0.0365). - For a household in the poorest quintile (
wealth_rank = 1) with only1ANC visit andHb = 9.5 g/dL, risk jumps to25.70%(prediction = 0.2570). - Exact Shapley values decompose that
+22.05percentage-point surge cleanly:+18.39 ppfromwealth_rank,+4.08 ppfrom lowanc_visits, and-0.43 ppfrommaternal_hb_g_dl. - Production Bridge (
shapLibrary): For \(p = 3\) features, \(3! = 6\) permutations is instant; for \(p = 30\) features, \(30! \approx 2.65 \times 10^{32}\) is impossible by brute force! In full research pipelines, theshaplibrary’s TreeSHAP (shap.TreeExplainer(model)) exploits tree path structure to compute these exact same Shapley values in polynomial time \(O(T L D^2)\).
24.3.8 Break It On Purpose! (Tree & Ensemble Error Anatomy)
max_depth=0)
Python’s Traceback:
Diagnosis: InvalidParameterError (a subclass of ValueError) fires because a decision tree must have at least depth 1 (a decision stump) or None (grow until leaves are pure). Setting max_depth=0 leaves zero splits possible.
Fix: Use max_depth >= 1 (e.g., 1 for a stump, 3..6 for regularized trees) or max_depth=None when intentionally testing an unpruned tree.
oob_score=True With bootstrap=False)
Python’s Traceback:
Diagnosis: Out-of-Bag scoring depends mathematically on bootstrap resampling with replacement leaving ~36.8% of training rows unused in each tree. When bootstrap=False, every tree sees 100% of rows, so no out-of-bag rows exist!
Fix: Keep the default bootstrap=True whenever oob_score=True, or use \(k\)-fold cross-validation if you deliberately disable bootstrapping.
NaNs Into Classic GradientBoostingClassifier)
Python’s Traceback:
Diagnosis: Classic GradientBoostingClassifier (like LogisticRegression and pre-1.4 versions of RandomForestClassifier) refuses NaN inputs and crashes during .fit().
Fix: Either wrap the estimator in a leakage-free Pipeline([("imputer", SimpleImputer(strategy="median")), ("clf", ...)]), drop incomplete rows intentionally, or switch directly to HistGradientBoostingClassifier, which learns optimal left/right branches for missing values natively.
24.4 Stage 4 — The Researcher’s Problem-Structuring Blueprint
Scenario: Your maternal-and-child health research team needs a single reproducible benchmark function run_stunting_tree_audit(raw_df, seed=42) that takes raw mch_survey_sample (without mutating it), cleans all survey artifacts, compares Decision Tree, Random Forest, and Histogram Gradient Boosting on a stratified 75/25 split using both ROC-AUC and PR-AUC, and identifies the top held-out risk driver via permutation importance on the winning model.
24.4.1 Blueprint (Completed on Paper Before Coding)
Hygiene & Feature Engineering (Zero Mutation):
- Copy
raw_dffirst (work = raw_df.copy()). - Standardize
residencecasing, replaceanc_visits == 999withnp.nan, derive binaryis_urban, map ordinalwealth_quintile(Poorest: 1 .. Richest: 5) towealth_rank, and drop incomplete rows across the 5 features +child_stunted.
- Copy
Stratified Multi-Family Benchmark:
- Split 75/25 stratified on
child_stuntedwithrandom_state=seed. - Fit
DecisionTreeClassifier(max_depth=5),RandomForestClassifier(n_estimators=100, max_depth=6), andHistGradientBoostingClassifier(max_depth=4), scoring test probabilities ([:, 1]) withroc_auc_scoreandaverage_precision_score. - Sort the leaderboard descending by
roc_auc.
- Split 75/25 stratified on
Held-Out Interpretability Audit & Verification:
- Run
permutation_importance(scoring="roc_auc", n_repeats=10) on the winning fitted model using(X_test, y_test)to extracttop_feature. - Verify complete-case size (
n = 470), confirm the winning model empirically rather than assuming boosting always wins on small \(n\), and check thatwealth_rankemerges as the top held-out driver.
- Run
24.4.2 Clean Python Implementation (Verbatim in 01_walkthrough.ipynb)
def run_stunting_tree_audit(raw_df, seed=42):
"""Clean MCH data, benchmark 3 tree models, and audit top driver."""
work = raw_df.copy()
work["residence"] = (
work["residence"].astype(str).str.strip().str.title()
)
work["anc_visits"] = work["anc_visits"].replace(999, np.nan)
work["is_urban"] = (work["residence"] == "Urban").astype(int)
w_map = {
"Poorest": 1, "Poorer": 2, "Middle": 3,
"Richer": 4, "Richest": 5,
}
work["wealth_rank"] = (
work["wealth_quintile"].astype(str).str.strip().str.title()
.map(w_map)
)
feats = [
"maternal_age_years", "anc_visits", "maternal_hb_g_dl",
"is_urban", "wealth_rank",
]
df = work[feats + ["child_stunted"]].dropna()
X_tr, X_te, y_tr, y_te = train_test_split(
df[feats], df["child_stunted"].astype(int),
test_size=0.25, stratify=df["child_stunted"], random_state=seed,
)
models = [
("DecisionTree", DecisionTreeClassifier(
max_depth=5, random_state=seed)),
("RandomForest", RandomForestClassifier(
n_estimators=100, max_depth=6, random_state=seed)),
("HistGradientBoosting", HistGradientBoostingClassifier(
max_depth=4, random_state=seed)),
]
rows, fitted = [], {}
for name, clf in models:
clf.fit(X_tr, y_tr)
proba = clf.predict_proba(X_te)[:, 1]
rows.append({
"model": name,
"roc_auc": round(float(roc_auc_score(y_te, proba)), 4),
"pr_auc": round(
float(average_precision_score(y_te, proba)), 4
),
})
fitted[name] = clf
board = (
pd.DataFrame(rows)
.sort_values("roc_auc", ascending=False)
.reset_index(drop=True)
)
winner = str(board.iloc[0]["model"])
pi = permutation_importance(
fitted[winner], X_te, y_te, scoring="roc_auc",
n_repeats=10, random_state=seed,
)
top_feat = feats[int(np.argmax(pi.importances_mean))]
return board, winner, top_feat
board, winner, top_feat = run_stunting_tree_audit(mch_raw, seed=42)
print(board.to_string(index=False))
print(f"Winning model: {winner} | Top held-out driver: {top_feat}")Output:
Notice the authentic scientific takeaway: on a small complete-case survey (\(n = 470\), \(118\) test households), a regularized shallow DecisionTree(max_depth=5) actually outperforms default ensembles on this split—reminding us why No Free Lunch holds and why every manuscript must benchmark simple baselines fairly!
24.5 Stage 5 — Guided “To-Do” Practice Labs
Open part_05_classical_ml/ch24_trees_ensembles_interpretability/02_guided_todos.ipynb in Google Colab, run Step 0 to activate sharukh-tutor, and implement all six functions below. Pass each function name (without parentheses!) to tutor.check(...).
- To-Do 24.1 (
gini_impurity(y)): Compute Gini impurity \(1 - \sum p_k^2\) in pure NumPy rounded to 4 decimals, returning0.0for an empty array. - To-Do 24.2 (
shannon_entropy(y)): Compute base-2 Shannon entropy \(-\sum p_k \log_2(p_k)\) rounded to 4 decimals, returning0.0for pure or empty arrays. - To-Do 24.3 (
best_stump_split(x_feature, y)): Test all midpoints between consecutive sorted unique values ofx_featureto minimize weighted Gini impurity (x <= tvs.x > t), returning(best_thresh, best_weighted_gini)rounded to 4 decimals. - To-Do 24.4 (
compare_tree_depths(X_train, y_train, X_val, y_val, depths, seed=42)): FitDecisionTreeClassifier(max_depth=d, random_state=seed)acrossdepthsand return apd.DataFramewith columns["max_depth", "train_auc", "val_auc", "gap"]rounded to 4 decimals. - To-Do 24.5 (
fit_ensemble_comparison(X_train, y_train, X_test, y_test, seed=42)): Fit"DecisionTree"(max_depth=5),"RandomForest"(n_estimators=100, max_depth=6), and"HistGradientBoosting"(max_depth=4), all withrandom_state=seed, returning apd.DataFramewith columns["model", "roc_auc", "pr_auc"]rounded to 4 decimals. - To-Do 24.6 (
compute_permutation_ranking(model, X_test, y_test, feature_names, seed=42)): Runpermutation_importance(..., scoring="roc_auc", n_repeats=10, random_state=seed)and return apd.DataFramewith columns["feature", "importance_mean", "importance_std"]sorted byimportance_meandescending and rounded to 4 decimals.
24.6 Stage 6 — Concept & Execution Trace Test (No Computer Allowed!)
Work through all 10 tree, ensemble, and interpretability questions on paper before checking Stage 8:
- Gini vs. Entropy Hand Calculation: A leaf node contains
3stunted children and1non-stunted child (\(p = [0.75, 0.25]\)). Compute its exact Gini impurity by hand, and state whether its base-2 Shannon entropy is less than, equal to, or greater than1.0bit. - Midpoint Split Candidate: A sorted feature vector has unique values
[2.0, 4.0, 10.0]with labels[1, 0, 0]. What exact midpoint threshold \(t\) produces a weighted Gini impurity of0.0? - Reading an Overfitting Table: At
max_depth=12, a Decision Tree hitstrain_auc = 0.9993andval_auc = 0.5809, whereasmax_depth=5hitstrain_auc = 0.8854andval_auc = 0.6550. Is the depth-12 tree suffering from high bias or high variance, and why? - Bootstrap OOB Mathematics: When a Random Forest draws a bootstrap sample of size \(n\) with replacement from a large cohort, approximately what percentage of training rows are Out-of-Bag (OOB) for any single tree—and why does setting
bootstrap=Falsebreakoob_score=True? - Why
max_features="sqrt"Matters: Suppose one dominant predictor (wealth_rank) always yields the best root split. If you build 100 bagged trees using all features (max_features=None) vs.max_features="sqrt", which forest produces more decorrelated trees and better variance reduction? - Bagging vs. Boosting Parallelism: Can the 100 trees inside
RandomForestClassifierbe trained completely in parallel across CPU cores? What about the 100 boosting iterations insideHistGradientBoostingClassifier? Explain why. - Gradient Boosting Learning Rate Trade-Off: If you shrink the boosting learning rate \(\eta\) from
0.1to0.01, what must you generally do tomax_iter(the number of boosting trees) to avoid underfitting? - Unmasking MDI Feature-Importance Bias: A student adds a random 10-digit hospital registration ID column to a Random Forest and sees it rank #1 in
rf.feature_importances_. Explain why default MDI made this error and which Scikit-Learn tool catches it. - Exact 2-Feature Shapley Hand Calculation: Suppose \(f(x_1, x_2) = 1.0 + 2 x_1 + 4 x_1 x_2\), with
baseline = [0, 0]andx = [1, 1]. Trace both permutations(0, 1)and(1, 0)by hand to compute exact \(\phi_0\), \(\phi_1\), \(\phi_2\), and verify the Efficiency Axiom \(\phi_0 + \phi_1 + \phi_2 = f(x)\). - Native
NaNError Anatomy: Your dataset contains missing hemoglobin values (np.nan). You callGradientBoostingClassifier().fit(X, y)and Python raisesValueError: Input X contains NaN. Name two valid engineering fixes (one using a different tree estimator and one using preprocessing).
24.7 Stage 7 — Chapter 24 Coding Exam (100 Points)
Open part_05_classical_ml/ch24_trees_ensembles_interpretability/03_coding_exam.ipynb, implement all three functions, and run tutor.check_exam("ch24", exam_submissions) to earn your Chapter 24 score out of 100 points.
24.7.1 Exam Task 24.1 (30 Points): exact_shapley_3features(predict_fn, x_row, baseline_row)
Without mutating x_row or baseline_row, where predict_fn(v) accepts a 1D 3-element numeric vector and returns a scalar float:
- Evaluate
base_value = predict_fn(baseline_row)andprediction = predict_fn(x_row). - Average each feature’s marginal contribution across all \(3! = 6\) permutations of
[0, 1, 2]. - Return
{"base_value": round4, "shap_values": [round4, round4, round4], "prediction": round4}satisfying \(\phi_0 + \sum_i \phi_i \approx f(x)\).
24.7.2 Exam Task 24.2 (35 Points): tune_random_forest_oob(X_train, y_train, max_depths, seed=42)
Evaluate RandomForestClassifier(n_estimators=120, max_depth=d, oob_score=True, random_state=seed) across every depth d in max_depths:
- Construct
oob_tableas apd.DataFramewith columns["max_depth", "oob_score"]rounded to 4 decimals. - Return
{"best_depth": int, "best_oob_score": round4, "oob_table": oob_table}selecting the depth with maximumoob_score.
24.7.3 Exam Task 24.3 (35 Points): public_health_tree_benchmark(mch_df, seed=42)
Without mutating mch_df:
- Clean
residencecasing, replaceanc_visits == 999withnp.nan, create binaryis_urbanand ordinalwealth_rank(Poorest: 1 .. Richest: 5), and.dropna()across["maternal_age_years", "anc_visits", "maternal_hb_g_dl", "is_urban", "wealth_rank", "child_stunted"]. - Split 75/25 stratified on
child_stuntedwithrandom_state=seed. - Fit
"DecisionTree"(max_depth=5),"RandomForest"(n_estimators=100, max_depth=6), and"HistGradientBoosting"(max_depth=4), all withrandom_state=seed, buildingleaderboard(["model", "roc_auc", "pr_auc"]) sorted byroc_aucdescending (4 decimals). - Compute held-out
permutation_importance(scoring="roc_auc", n_repeats=10, random_state=seed) on the winningbest_modelto extracttop_feature, and return{"leaderboard": leaderboard, "best_model": best_model, "top_feature": top_feature}.
24.8 Stage 8 — Complete Annotated Solutions & Debugging Post-Mortem
24.8.1 A. Concept & Execution Trace Test Key
- Gini: \(1 - (0.75^2 + 0.25^2) = 1 - (0.5625 + 0.0625) = \mathbf{0.375}\). Entropy: Less than
1.0bit (specifically0.8113bits), because binary Shannon entropy achieves its maximum of1.0bit only at a 50/50 split. - Midpoints are
(2.0 + 4.0) / 2 = 3.0and(4.0 + 10.0) / 2 = 7.0. Thresholdt = 3.0places[1]on the left (pure, Gini =0.0) and[0, 0]on the right (pure, Gini =0.0), giving weighted Gini0.0. - High variance (overfitting): The depth-12 tree has near-zero training error (
0.9993) because it memorized idiosyncratic training noise, causing test AUC to collapse (0.5809). - Approximately
36.8%(\(1/e\)) of training rows are Out-of-Bag for any single bootstrap tree. Settingbootstrap=Falsetrains every tree on 100% of training rows, leaving zero OOB rows to evaluate and triggeringValueError. max_features="sqrt"produces much better decorrelation! Withmax_features=None, nearly every tree splits onwealth_rankat the root, yielding highly correlated trees whose average barely reduces variance.RandomForestClassifier: Yes, all 100 bootstrap trees are completely independent and train in parallel (n_jobs=-1).HistGradientBoostingClassifier: No across trees, because tree \(m\) depends sequentially on the residuals of tree \(m - 1\) (parallelism happens within each tree’s histogram bin search instead).- You must increase
max_iter(train more boosting rounds), because each smaller step \(\eta = 0.01\) corrects only 1% of the residual per tree. - Default MDI sums training-set impurity reductions across splits; a unique 10-digit ID offers \(n - 1\) split thresholds to memorize training labels. Held-out
sklearn.inspection.permutation_importanceon(X_test, y_test)exposes the ID immediately by showing near-zero (or negative) test-AUC drop. - \(\phi_0 = f(0, 0) = \mathbf{1.0}\) and \(f(1, 1) = 1 + 2 + 4 = \mathbf{7.0}\).
- Order
(0, 1): reveal \(x_1\): \(f(1, 0) - f(0, 0) = 3 - 1 = +2\); reveal \(x_2\): \(f(1, 1) - f(1, 0) = 7 - 3 = +4\). - Order
(1, 0): reveal \(x_2\): \(f(0, 1) - f(0, 0) = 1 - 1 = 0\); reveal \(x_1\): \(f(1, 1) - f(0, 1) = 7 - 1 = +6\). - Averaging both orders: \(\phi_1 = (2 + 6)/2 = \mathbf{4.0}\), \(\phi_2 = (4 + 0)/2 = \mathbf{2.0}\). Efficiency check: \(1.0 + 4.0 + 2.0 = \mathbf{7.0} = f(1, 1)\)!
- Order
- Switch to
HistGradientBoostingClassifier, which learns optimal missing-value branch routing natively; or (b) wrapGradientBoostingClassifierinside aPipelinepreceded bySimpleImputer(strategy="median")fit strictly onX_train.
- Switch to
24.8.2 B. Annotated Solutions — Guided To-Dos (Mirrored in 02_guided_todos_SOLUTION.ipynb)
import numpy as np
import pandas as pd
from sklearn.ensemble import (
HistGradientBoostingClassifier,
RandomForestClassifier,
)
from sklearn.inspection import permutation_importance
from sklearn.metrics import average_precision_score, roc_auc_score
from sklearn.tree import DecisionTreeClassifier
# To-Do 24.1
def gini_impurity(y):
"""Compute Gini impurity 1 - sum(p_k^2) rounded to 4 decimals."""
arr = np.asarray(y)
if arr.size == 0:
return 0.0
_, counts = np.unique(arr, return_counts=True)
probs = counts / arr.size
return round(float(1.0 - np.sum(probs ** 2)), 4)
# To-Do 24.2
def shannon_entropy(y):
"""Compute base-2 Shannon entropy rounded to 4 decimals."""
arr = np.asarray(y)
if arr.size == 0:
return 0.0
_, counts = np.unique(arr, return_counts=True)
probs = counts / arr.size
probs = probs[probs > 0]
ent = float(-np.sum(probs * np.log2(probs)))
return round(0.0 if ent == 0.0 else ent, 4)
# To-Do 24.3
def best_stump_split(x_feature, y):
"""Find midpoint threshold minimizing weighted Gini impurity."""
def _gini(arr):
if arr.size == 0:
return 0.0
_, counts = np.unique(arr, return_counts=True)
probs = counts / arr.size
return float(1.0 - np.sum(probs ** 2))
x = np.asarray(x_feature, dtype=float)
y_arr = np.asarray(y)
uniq = np.unique(x)
mids = (uniq[:-1] + uniq[1:]) / 2.0
best_t, best_g = float(mids[0]), float("inf")
n = y_arr.size
for t in mids:
left = y_arr[x <= t]
right = y_arr[x > t]
wg = (left.size / n) * _gini(left) + (right.size / n) * _gini(right)
if wg < best_g:
best_g = wg
best_t = float(t)
return (round(best_t, 4), round(best_g, 4))
# To-Do 24.4
def compare_tree_depths(X_train, y_train, X_val, y_val, depths, seed=42):
"""Compare DecisionTree train/val ROC-AUC and gap across depths."""
rows = []
for d in depths:
clf = DecisionTreeClassifier(max_depth=d, random_state=seed)
clf.fit(X_train, y_train)
tr = float(roc_auc_score(y_train, clf.predict_proba(X_train)[:, 1]))
va = float(roc_auc_score(y_val, clf.predict_proba(X_val)[:, 1]))
rows.append({
"max_depth": d,
"train_auc": round(tr, 4),
"val_auc": round(va, 4),
"gap": round(tr - va, 4),
})
return pd.DataFrame(rows)
# To-Do 24.5
def fit_ensemble_comparison(X_train, y_train, X_test, y_test, seed=42):
"""Fit DecisionTree, RandomForest, and HistGradientBoosting."""
models = [
("DecisionTree", DecisionTreeClassifier(
max_depth=5, random_state=seed)),
("RandomForest", RandomForestClassifier(
n_estimators=100, max_depth=6, random_state=seed)),
("HistGradientBoosting", HistGradientBoostingClassifier(
max_depth=4, random_state=seed)),
]
rows = []
for name, clf in models:
clf.fit(X_train, y_train)
proba = clf.predict_proba(X_test)[:, 1]
rows.append({
"model": name,
"roc_auc": round(float(roc_auc_score(y_test, proba)), 4),
"pr_auc": round(
float(average_precision_score(y_test, proba)), 4
),
})
return pd.DataFrame(rows)
# To-Do 24.6
def compute_permutation_ranking(
model, X_test, y_test, feature_names, seed=42
):
"""Rank features by held-out ROC-AUC permutation importance."""
res = permutation_importance(
model, X_test, y_test, scoring="roc_auc",
n_repeats=10, random_state=seed,
)
df = pd.DataFrame({
"feature": list(feature_names),
"importance_mean": np.round(res.importances_mean, 4),
"importance_std": np.round(res.importances_std, 4),
})
return df.sort_values(
"importance_mean", ascending=False
).reset_index(drop=True)24.8.3 C. Annotated Solutions — Chapter 24 Coding Exam (Mirrored in 03_coding_exam_SOLUTION.ipynb)
import itertools
from sklearn.model_selection import train_test_split
# Exam Task 24.1 (30 pts)
def exact_shapley_3features(predict_fn, x_row, baseline_row):
"""Compute exact 3-feature Shapley values over all 6 permutations."""
x = np.asarray(x_row, dtype=float)
base = np.asarray(baseline_row, dtype=float)
base_val = float(predict_fn(base))
pred_val = float(predict_fn(x))
contribs = np.zeros(3, dtype=float)
perms = list(itertools.permutations([0, 1, 2]))
for perm in perms:
state = base.copy()
prev = base_val
for idx in perm:
state[idx] = x[idx]
curr = float(predict_fn(state))
contribs[idx] += curr - prev
prev = curr
shap_vals = contribs / len(perms)
return {
"base_value": round(base_val, 4),
"shap_values": [round(float(v), 4) for v in shap_vals],
"prediction": round(pred_val, 4),
}
# Exam Task 24.2 (35 pts)
def tune_random_forest_oob(X_train, y_train, max_depths, seed=42):
"""Select best RandomForest max_depth using Out-of-Bag accuracy."""
rows = []
best_depth = int(max_depths[0])
best_oob = -1.0
for d in max_depths:
rf = RandomForestClassifier(
n_estimators=120, max_depth=d,
oob_score=True, random_state=seed,
).fit(X_train, y_train)
score = float(rf.oob_score_)
rows.append({"max_depth": int(d), "oob_score": round(score, 4)})
if score > best_oob:
best_oob = score
best_depth = int(d)
return {
"best_depth": best_depth,
"best_oob_score": round(best_oob, 4),
"oob_table": pd.DataFrame(rows),
}
# Exam Task 24.3 (35 pts)
def public_health_tree_benchmark(mch_df, seed=42):
"""Benchmark 3 tree families on MCH stunting and rank top driver."""
work = mch_df.copy()
work["residence"] = (
work["residence"].astype(str).str.strip().str.title()
)
work["anc_visits"] = work["anc_visits"].replace(999, np.nan)
work["is_urban"] = (work["residence"] == "Urban").astype(int)
w_map = {
"Poorest": 1, "Poorer": 2, "Middle": 3,
"Richer": 4, "Richest": 5,
}
work["wealth_rank"] = (
work["wealth_quintile"].astype(str).str.strip().str.title()
.map(w_map)
)
feats = [
"maternal_age_years", "anc_visits", "maternal_hb_g_dl",
"is_urban", "wealth_rank",
]
clean = work[feats + ["child_stunted"]].dropna()
X = clean[feats]
y = clean["child_stunted"].astype(int)
X_tr, X_te, y_tr, y_te = train_test_split(
X, y, test_size=0.25, stratify=y, random_state=seed
)
specs = [
("DecisionTree", DecisionTreeClassifier(
max_depth=5, random_state=seed)),
("RandomForest", RandomForestClassifier(
n_estimators=100, max_depth=6, random_state=seed)),
("HistGradientBoosting", HistGradientBoostingClassifier(
max_depth=4, random_state=seed)),
]
rows, fitted = [], {}
for name, clf in specs:
clf.fit(X_tr, y_tr)
proba = clf.predict_proba(X_te)[:, 1]
rows.append({
"model": name,
"roc_auc": round(float(roc_auc_score(y_te, proba)), 4),
"pr_auc": round(
float(average_precision_score(y_te, proba)), 4
),
})
fitted[name] = clf
leaderboard = (
pd.DataFrame(rows)
.sort_values("roc_auc", ascending=False)
.reset_index(drop=True)
)
best_model = str(leaderboard.iloc[0]["model"])
pi = permutation_importance(
fitted[best_model], X_te, y_te, scoring="roc_auc",
n_repeats=10, random_state=seed,
)
top_feature = feats[int(np.argmax(pi.importances_mean))]
return {
"leaderboard": leaderboard,
"best_model": best_model,
"top_feature": top_feature,
}24.8.4 D. Debugging Post-Mortem: Top 3 Chapter 24 Failure Modes
- Passing Hard Class Predictions (
.predict) Instead of Probabilities (.predict_proba[:, 1]) Into ROC-AUC / PR-AUC: Hard binary labels collapse all probability thresholds to a single operating point, distorting bothroc_auc_scoreandaverage_precision_score. Always slice column1of.predict_proba(X). - Trusting Default MDI
.feature_importances_on Mixed-Cardinality Clinical Data: Training-set impurity gain rewards continuous or ID-like columns with many split thresholds—even pure noise! Always validate global driver rankings with held-outpermutation_importanceon(X_test, y_test). - Evaluating Only One Feature Order When Explaining Nonlinear Models: In models with feature interactions, changing which feature is turned on first shifts marginal credit dramatically. Averaging across permutations (exact Shapley / TreeSHAP) is the only way to preserve fair attribution and guarantee the Efficiency Axiom \(\phi_0 + \sum_i \phi_i = f(x)\).


