Decision Trees (CART): Gini, Entropy, Variance Reduction & Pruning
Axis-aligned recursive partitioning, Gini vs. Entropy splits, why trees ignore monotonic scaling, why they cannot extrapolate, and Cost-Complexity Pruning.
Decision Trees recursively split data into purer subsets using simple if-else threshold questions chosen by Gini Impurity, Entropy, or Variance Reduction.
Trees naturally model non-linear interactions, require zero feature scaling, are human-readable, and serve as the building block of Random Forests and XGBoost.
Decision Trees are the atomic Lego brick of Random Forests, XGBoost, LightGBM, and CatBoost!
You rarely ship a single deep tree in production — you ensemble hundreds of them. Mastering how CART chooses splits, handles missing values, and prunes leaves is mandatory to debug XGBoost and LightGBM.
Trees handle non-linear feature interactions automatically without scaling, making shallow trees (depth 3–4) gold-standard interpretable rules for finance, credit, and clinical policy.
Classic interview trio: 'Gini vs. Entropy — when do they differ? Why can't trees extrapolate? And why doesn't StandardScaler change a tree's splits?'
You train a Decision Tree Regressor on house prices ranging from $100k to $900k (1,000–4,000 sqft). At test time, a 10,000 sqft mansion arrives. What will the tree predict?
- Compute Gini Impurity, Entropy, and MSE Variance Reduction for candidate CART splits
- Explain why Decision Trees are invariant to monotonic feature scaling (StandardScaler not needed)
- Diagnose why single trees are high-variance learners and why they cannot extrapolate beyond training target bounds
- Control overfitting with max_depth, min_samples_leaf, and Cost-Complexity Pruning (ccp_alpha)
A Decision Tree plays a game of 20 Questions with your data. At every node, it greedily asks the single yes/no question (feature ≤ threshold) that makes the two child buckets as pure (all one class, or lowest target variance) as possible!
1. Recursive Axis-Aligned Splitting & Scale Invariance
Because a tree only asks rank-order questions (Income ≤ $80,000), applying any strictly increasing transform (log(Income), StandardScaler, Income × 100) keeps the exact same sorted order of rows — producing the exact same tree!
Classification vs. Regression Split Criteria
- Classification (Gini Impurity): . Fast (no logarithms) — default in
scikit-learn. - Classification (Entropy / Information Gain): .
- Regression (Variance Reduction): Pick the split minimizing weighted child MSE: .
Slide Split Threshold x ≤ 3.5 to Maximize Gini Gain
2. Gini vs. Entropy & Why Greedy Splits Are Local
Interviewers love asking whether Gini or Entropy is better. Plot both curves side-by-side (with Entropy scaled by ½) and you'll see they are almost twins — agreeing on ~98% of splits!
Practical Differences
- Gini is slightly faster to compute and tends to isolate the most frequent class in its own branch.
- Entropy tends to produce slightly more balanced trees.
- Greedy limitation: Finding the globally optimal tree is NP-Complete, so CART uses greedy top-down splitting — meaning small perturbations in training rows can flip the root split and yield a totally different tree (High Variance!).
3. Pre-Pruning vs. Cost-Complexity Post-Pruning
Left unconstrained, a CART tree keeps splitting until every leaf has 1 row (100% training accuracy, terrible test accuracy!). You stop memorization either before growing (pre-pruning) or by trimming back weak branches after growing (ccp_alpha).
Weakest-Link Cost-Complexity Pruning
As increases, subtrees that don't reduce impurity enough to justify their leaf count are pruned away — pick via cross-validation!
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
Because every CART split is perpendicular to a single axis (x₁ ≤ t or x₂ ≤ t), it has to approximate a clean 45° diagonal line using dozens of tiny jagged staircase steps!
Tune max_depth & min_samples_leaf to Find the Generalization Sweet Spot
Credit Policy & Rule Extraction
Shallow depth-3 trees give regulators auditable if/else approval logic.
Base Learners for Ensembles
Deep unpruned trees power Random Forests; shallow depth-4–6 trees power XGBoost/LightGBM.
from sklearn.tree import DecisionTreeClassifier
# Pre-pruned + Cost-Complexity pruned CART tree
tree = DecisionTreeClassifier(
criterion="gini",
max_depth=5,
min_samples_leaf=10,
ccp_alpha=0.005,
random_state=42,
)
tree.fit(X_train, y_train)
max_depth=None).