Machine Learning · Insper

Random Forest

Advanced approach

Where averaging stops helping, which knobs matter, and how (not) to read a forest

The path of this part

  1. Why the vote worksCondorcet's jury; the floor ρσ², measured
  2. Tune what mattersmax_features, min_samples_leaf, enough trees
  3. Read the importancesimpurity bias, permutation, shared credit
  4. Variants and limitsExtra-Trees, no extrapolation, cost
Each stage answers one question: when does a vote of many beat one? → which knobs are worth tuning? → which features does the forest use, and can we trust the answer? → what can a forest not do, and what does it cost?
Stage 1 · when does a vote of many beat one?

Condorcet's jury (1785)

Each juror is right with \(p = 0.6\), independently. With three: \(0.6^3 + 3 \times 0.6^2 \times 0.4 = 0.648\).

voters1111011,001
independent (\(c = 0\))0.6010.7550.9801.000
copy with prob. \(c = 0.1\)0.6010.7430.9170.998
copy with prob. \(c = 0.3\)0.6010.6780.6180.597
With \(c = 0.3\), two voters correlate at only 0.09 — and a committee of 1,001 is right as often as a single voter: the copying block decides every vote.

Why bagging only halved the variance

  • One bagged tree: variance \(\sigma^2 = 0.0937\).
  • Two of them, at the same point: correlation \(\rho = 0.44\) — they all saw the same 100 points, noise included.
\[ \rho\sigma^2 + \frac{(1-\rho)\,\sigma^2}{100} = 0.44 \times 0.0937 + \frac{0.56 \times 0.0937}{100} = 0.0417 \]

Measured: 0.0413.

Almost all the remaining variance is the floor \(\rho\sigma^2\). More trees do not help; less correlated trees do — the job of max_features.
Stage 2 · which knobs are worth tuning?

Hyperparameters: one at a time, on digits

knobvalues (CV accuracy)range
n_estimators10: 0.941 · 100: 0.976 · 300: 0.9770.036
max_features1: 0.970 · sqrt: 0.977 · None: 0.9470.030
min_samples_leaf1: 0.977 · 10: 0.947 · 30: 0.9150.062
max_depth3: 0.888 · 10: 0.977 · None: 0.9770.089
criteriongini: 0.977 · entropy: 0.9790.002
Forests want deep trees. Tune max_features and min_samples_leaf; pick "enough trees" from the OOB curve; the rest barely matters.
Stage 3 · which features does the forest use, and can we trust the answer?

Impurity importance: biased

German credit + two columns of pure noise:

featuredistinct valuesMDI rankpermutation rank
credit_amount92118
random_id (noise)1,000214
checking_status451
coin (noise)21722
MDI is measured on the training data and rewards columns with many thresholds, which fit noise deep in the trees. Prefer permutation on held-out data.

Correlated features share the credit

MDIpermutation
checking_status, alone0.0860.144
checking_status, with a twin0.0570.049
checking_copy0.0590.054
Shuffling one copy lets the other do the work. A feature that looks unimportant may just have a substitute: permute the whole group at once.
Stage 4 · what can a forest not do, and what does it cost?

Extra-Trees, and what a forest cannot do

Extra-Trees

Random thresholds instead of searched ones; no bootstrap. On digits: 0.981 against the forest's 0.977 (a tie), with twice the nodes per tree.

No extrapolation

Trained on \(y = 3x\), \(0 \le x \le 10\): it predicts 29.6 at \(x = 12\) and at \(x = 20\). The linear model: 59.7 (truth: 60).

Every prediction is an average of training targets: outside the range seen, the forest goes flat.

The cost of a forest

digitsaccuracynodessaved size
one tree0.8292930.04 MB
forest, 300 trees0.97798,69614.32 MB
forest, leaves ≥ 30.97165,7549.57 MB
Bagging builds trees independently, in parallel, and cuts variance. Boosting (next lesson) builds them in sequence and cuts bias.
Advanced lab · about 30 min, in pairs

Tune, pick the threshold and test, steps 8 to 11

  • Same notebook, same session: the steps use X_train, y_train, cv, folds and roc_auc_score.
  • Tune max_features and min_samples_leaf; a threshold that sends at most 1% of legitimate e-mail to spam, chosen out-of-bag; the test, once; where the forest looks.
  • Notebook on Colab (in Portuguese), from step 8.
  • Simulations: the search for free, with OOB (step 8) and the importance of a pair of twins (step 11).

Pace: step 8 and the first simulation in 10 min; steps 9 and 10 in 10 min; step 11 and the second in 10 min.