←Back to 03 / Published Articles
03 / Resources/Tree Ensembles · Decision Trees & Random Forests
Interactive Diagnostic Essay · Topic 06

Decision Trees & Random Forests

The most human model in machine learning: a flowchart that asks yes/no questions until it is confident. Watch data fall through the branches, learn how splits are chosen, and see why a forest beats a tree.

01 / Watch Data Fall

The Ball-Drop Classifier

Think of a decision tree as a Plinko machine for data. Every student is a ball — orange balls pass, ink balls fail — and every gate is a yes/no question. The ball falls, the gate deflects it left or right, and by the time it reaches a bucket, all balls in that bucket share a label. The tree learns by choosing questions that make each bucket as pure as possible.

PASS studentFAIL student
looping forever · 4 balls per cycle
Hours > 2?Assignments > 5?Attendance > 60%?noyesyesnonoyesPASSFAILFAILPASS

Every bucket is a leaf · every gate is a learned question

02 / How the Questions Are Chosen

Impurity, Information Gain & Greedy Splitting

A tree is grown top-down, greedily: at every node the algorithm tries every feature and every threshold, and keeps the split that produces the purest children. Purity is measured by:

Gini ImpurityG=1−∑kpk2G = 1 - \sum_{k} p_k^2

Probability of mislabeling a random sample. 0 = pure node.

Entropy & Information GainH=−∑kpklog⁡2pkIG=H(parent)−nLnH(left)−nRnH(right)H = -\sum_{k} p_k \log_2 p_k \quad\quad \text{IG} = H(\text{parent}) - \tfrac{n_L}{n} H(\text{left}) - \tfrac{n_R}{n} H(\text{right})

Gini and entropy behave almost identically in practice — scikit-learn defaults to Gini because it skips a logarithm.

Worked example: a node holds 4 pass + 4 fail (Gini = 0.5). Split on “Assignments > 5?” and the children hold 4+0 and 0+4 — Gini drops to 0. That information gain of 0.5 is why the tree asks this question first.

ONE SPLIT, TWO PURE CHILDRENGini 0.50 → 0.00
parent · 4 pass + 4 failyesno4 pass · Gini 04 fail · Gini 0

Repeat until pure — or until told to stop.

03 / Entropy, Information Gain & ID3

Measuring Surprise in Bits

Gini is one way to score a split; the original one is entropy. Entropy is the expected surprise of a random draw from the node. A bucket that holds one class surprises nobody; a 50/50 bucket is a coin flip every single time.

Shannon EntropyH(S)=−∑k=1Kpklog⁡2pkH(S) = -\sum_{k=1}^{K} p_k \log_2 p_k

p_k is the share of class k in the node. The minus sign is only there because the logarithm of a probability is negative.

The base-2 logarithm is what makes the units bits. −log⁡2p-\log_2 p counts how many halvings it takes to narrow the field down to one outcome — a number of yes/no questions. Entropy is therefore a question budget, and since every split in a tree is a yes/no question, bits are its natural currency. Change the base and you only rescale that budget.

Information GainIG(S,A)=H(S)−∑v∣Sv∣∣S∣ H(Sv)\text{IG}(S, A) = H(S) - \sum_{v} \frac{|S_v|}{|S|}\, H(S_v)

The parent's entropy minus the size-weighted average of the children's entropies — the bits of uncertainty the question removes.

Read it as a subtraction. A question that leaves its children as confused as the parent gains nothing; one that separates the classes cleanly gains everything. Because the score is always in bits, ID3 can compare wildly different questions — a height threshold against a diameter threshold — on the same scale.

ENTROPY DRAINS · INFORMATION GAIN FILLS7s loop · H 1.00 → 0.00

The meter empties · the gain bar fills · then the split resets

Entropy anchors
0
Pure node

One class holds everything. No surprise left to remove, so the tree stops here.

1
50 / 50 binary

One full bit of uncertainty — the most a two-class node can ever carry.

1.585
Three uniform classes

log⁡23\log_2 3 — the ceiling for K equally likely classes is log⁡2K\log_2 K.

ID3 · the recursive recipe
1 · Score every question

Take the entropy of the node, then try every feature and every cutoff, computing the information gain of each candidate partition.

2 · Keep the best split

Choose the partition with the maximum information gain and freeze it into a decision node on that feature and value.

3 · Recurse

Send each subset down its own branch and repeat the whole procedure there — the same greedy question, one level deeper.

4 · Stop and label

Halt when a node is pure, features run out, or a depth or leaf-size limit is hit — then stamp the leaf with the majority class.

A note on information measures
GINI1−∑kpk21-\sum_k p_k^2Cheapest — no logarithm. The pragmatic default.
ENTROPY−∑kpklog⁡2pk-\sum_k p_k \log_2 p_kTheoretically motivated, measured in bits. Gentler on imbalanced data.
MISCLASSIFICATION1−max⁡kpk1-\max_k p_kCrudest — blind to how the remaining mass is distributed.

All three answer the same question — how mixed is this node — and in practice they usually crown the same winning split. When they disagree the gap is small; the real differences are cost to compute and how they behave at the extremes.

04 / When Memory Replaces Learning

Overfitting, Depth & Pruning

Left unchecked, a tree keeps splitting until every leaf contains one training point — a perfect scoreboard on the data it memorized, and a disaster on anything new. A deep tree has learned noise, not signal.

The counter-moves are all limits on growth:

max_depthfewer questions total
min_samples_splitno splitting tiny groups
min_samples_leafevery leaf needs evidence
ccp_alphaprune back after growing

In the animation, the orange train error only ever falls as depth grows — but the ink test error turns around and climbs. The sweet spot is the bottom of that U, and Topic 05's cross-validation is how we find it.

train errortest error
looping sweep over tree depth
tree depth (complexity) →error →sweet spotmemorization zone

Depth 1 = underfit · depth 20 = memorized the noise

Try it:→ 90% train / 87% test. Honest, slightly underfit.
05 / Wisdom of the Crowd

Random Forests: Bagging + Voting

ONE QUERY · THREE TREES · MAJORITY WINSlooping
new student →PASSFAILFAILPASSPASSFAILvotes:MAJORITY → PASS (2 vs 1)

Each tree sees a bootstrap sample & a random feature subset

One deep tree is a nervous genius; a forest of slightly wrong trees is a calm committee. A random forest trains hundreds of trees on deliberately corrupted views of the data so their mistakes don't correlate:

1 · Bootstrap (Bagging)

Each tree trains on a random sample with replacement — about 63% unique rows, the rest are duplicates. Every tree sees a slightly different dataset.

2 · Feature Randomness

At every split, only p\sqrt{p} random features may compete. This stops one dominant feature from making all trees identical.

3 · Aggregate

Classification = majority vote; regression = average of predictions. Variance shrinks roughly with 1/B1/\sqrt{B} for BB trees — while bias stays low.

Bonus: the ~37% of rows each tree never saw (out-of-bag) form a free validation set — cross-validation built into the training loop.

06 / Field Guide

When to Reach for Trees

SUPERPOWERS
  • · No scaling or normalization needed
  • · Handles mixed & missing data gracefully
  • · Captures non-linear interactions automatically
  • · Fully interpretable (single tree)
  • · Feature importance for free
KRYPTONITE
  • · Single trees overfit easily
  • · Axis-aligned splits struggle on smooth diagonal boundaries
  • · Forests lose the interpretability
  • · Can't extrapolate beyond training range
  • · Larger memory than linear models
KNOBS THAT MATTER
  • · n_estimators — more is safer, slower
  • · max_depth — the overfit dial
  • · max_features — decorrelation dial
  • · min_samples_leaf — smoothing dial
THE ROAD AHEAD

Gradient boosting (XGBoost, LightGBM) grows trees sequentially, each one correcting the previous ensemble's residuals — it is the same ball-drop machinery aimed at what the committee still gets wrong. And when features become images or sequences instead of a tidy table, trees struggle and the neural networks of Topics 07–09 take over.