Machine Learning ยท easy

Explain how a random forest works.

Asked in ML Engineer interviews, in the Machine Learning round.

Short answer

Bagged decision trees with feature subsampling, averaged or voted to reduce variance.

How to answer it

Definition

Many decision trees trained on different random views of the data, whose predictions are averaged for regression or voted for classification. The randomness is the point: it makes the trees' errors partly independent, so averaging cancels them.

The two sources of randomness

1. Bagging          each tree trains on a bootstrap sample of the ROWS
                    (sampled with replacement, so about 63% of rows appear)

2. Feature subsampling
                    at each split, only a random subset of COLUMNS is
                    considered (commonly sqrt(p) for classification)

The second one is what distinguishes a random forest from plain bagged trees. Without it, one dominant feature would sit at the top of nearly every tree and the trees would be highly correlated, so averaging would gain little.

Why averaging works

variance of the mean of n correlated variables
  = rho * var  +  (1 - rho) * var / n

The second term shrinks with more trees. The first does not, and it is set by rho, how correlated the trees are. That formula is the whole design: bagging and feature subsampling exist to push rho down so averaging can work.

Key points

FAQ

The 30-second answer

It trains many decision trees, each on a bootstrap sample of the rows, and at every split each tree only considers a random subset of the features. Then it averages the predictions for regression, or votes for classification.
Each individual tree is deliberately deep and overfits, so it has low bias and high variance. Averaging many of them cancels the variance, provided their errors are not too correlated, and that is exactly what the two sources of randomness are for. The feature subsampling in particular stops one strong predictor sitting at the top of every tree and making them all alike.
Practically it is a strong tabular baseline: no scaling needed, handles mixed types, and out-of-bag error gives validation almost for free. Adding more trees never overfits; depth and leaf size are what control that.

In the interview

Practice this for real