English static mirror for SEO/GEO · AI-assisted translation · Read Chinese original

AI Engineering Foundations 26: Decision Trees and Random Forests — One Tree Memorizes, a Hundred Trees Vote

Forum topic · 小凯 · 2026-10-21

Summary

This article is a study note on decision trees and random forests, based on Lesson 4 of Phase 2 of the AI Engineering from Scratch course. It explains why decision trees dominate tabular data, how Gini impurity and entropy guide splitting decisions, and which pruning gates (max depth, min samples per leaf) prevent memorization. It then dissects random forest's two randomness tricks: bootstrap sampling (with the 1 − 1/e ≈ 63.3% out-of-bag insight) and feature randomization, noting that scikit-learn's regression forests default to using all features, unlike the original 2001 paper's p/3 recommendation. Through hands-on experiments, the author tests common claims: larger test sets confirm that test accuracy stabilizes as trees increase, forests improve accuracy rather than smoothing test curves in this setup, and deep trees overfit in regression but not necessarily in classification. The final section exposes feature-importance pitfalls: MDI inflates high-cardinality noise features (25–27% importance for pure noise), and permutation importance must be computed on held-out data — shuffling on training data measures memorization, while shuffling on new data measures genuine understanding.

AI Engineering Foundations 26: Decision Trees and Random Forests — One Tree Memorizes, a Hundred Trees Vote

*Translated and condensed study note from Lesson 04 of Phase 2 of AI Engineering from Scratch (Rohit Ghumare, MIT License).*

Key points

  • Decision trees cut instead of draw lines. Where logistic regression draws a boundary line, a tree slices the feature space into rectangles by asking yes/no questions (age < 30? income > 50k? → approve loan). This interpretability plus tolerance for mixed numeric/categorical features is why gradient-boosted trees (XGBoost, LightGBM) dominate Kaggle tabular competitions, not neural networks.
  • Splitting criteria are interchangeable in practice. A 60/40 cat-dog box has Gini impurity 1 − (0.6² + 0.4²) = 0.48; entropy gives 0.971 bits. Across three depth settings, trees trained with either criterion differed by only 2.5–5 percentage points in accuracy — both are monotonic "more mixed = bigger" functions. Gini is preferred for speed (no logarithms).
  • Greedy growth needs brakes. Finding the globally optimal tree is NP-hard, so trees grow greedily by maximizing information gain (impurity before minus weighted impurity after). Without stopping conditions, a tree memorizes the training set. The course implements max depth and min-samples-per-leaf but not min-gain — visible evidence: a depth-3 demo tree contains a useless node ("feature 0 < −1.71") whose both leaves predict the same class, because tiny positive gains pass the "gain ≤ 0" cutoff.
  • The two drugs that cure a single tree

  • Bootstrap (bagging): each tree trains on a with-replacement sample; ~63.3% of distinct samples appear on average (1 − 1/e ≈ 0.632; measured 0.6335 over 2000 runs). The remaining ~37% are out-of-bag — a free validation set.
  • Feature randomization: each split only considers a random subset (√p for classification, p/3 for regression per the 2001 paper). Note: scikit-learn's regression forest defaults to all features, so out-of-the-box you only get bagging, not the paper's second ingredient.
  • A hand-written forest reached 0.88 vs 0.95 for scikit-learn at identical hyperparameters — the unimplemented production details are worth ~6–7 points.
  • Testing the textbook claims

  • "More trees never hurt" depends on sample size. With only 60 test samples, accuracy dropped from 0.95 (10 trees) to 0.8833 (50 trees) — one flipped sample is worth 1.67 points. With 1200 test samples, the same code shows a monotonic climb: 0.783, 0.870, 0.903, 0.906, 0.921.
  • "Forests are more stable" needs scrutiny. Over 5 perturbation rounds, single-tree and forest standard deviations were identical (0.0200). Over 30 rounds: 0.049 vs 0.046 — barely different. What improved was mean accuracy, 0.903 → 0.938. Bagging averages out sampling noise within one dataset, not the noise of changing the training data itself; here the forest buys accuracy, not a flatter curve.
  • Deep trees overfit in regression, not always in classification. The fully-grown classification tree hit 100% train accuracy and the best test score (0.85), while in regression, depth 5→10 pushed train error 0.029→0.003 but test error 0.043→0.060. Voting forgives useless deep splits; squared loss does not.
  • Feature importance: two rulers, one trap

  • MDI (Mean Decrease in Impurity) is biased toward high-cardinality features. On a crafted dataset where a binary feature truly determines the label and a 300-valued feature is pure noise, MDI assigned the noise 25% (27% via scikit-learn). More distinct values = more candidate thresholds = more lottery tickets to accidentally reduce impurity. With a perfectly clean label, the noise steals 0.0 — the bias needs both high cardinality and residual impurity.
  • Permutation importance must be measured on unseen data. Shuffling the noise feature on the training set drops accuracy by 4 points (destroying memorized answers); on held-out data it drops only 1.6, versus 37.8 for the true feature. Training-set permutation measures memorization; held-out permutation measures understanding.
  • The course's Exercise 5 (both rulers + one high-cardinality noise feature + untouched fresh data) packs all these pitfalls and remedies into a single exercise.

Tags

#decision-trees#random-forest#machine-learning#gini-impurity#feature-importance#permutation-importance#overfitting#scikit-learn

This page is an English static mirror generated for search and AI citation. It may be a full translation or structured summary of the Chinese original. Canonical interactive discussion lives on the Chinese page: https://zhichai.net/topic/178636258