← Notes

Random Forest vs XGBoost

5 min read

Random Forest vs XGBoost

A random forest is a collection of decision trees, but the trees are trained mostly independently and then combined.

A single decision tree

Start with tabular features:

text
age = 32
income = 140000
years_experience = 8
embedding_similarity = 0.82

The tree repeatedly asks questions:

text
embedding_similarity > 0.75?
├── yes
│   years_experience > 5?
│   ├── yes → leaf
│   └── no  → leaf
└── no
    income > 100000?
    ├── yes → leaf
    └── no  → leaf

The leaf contains a prediction.

For classification, a tree might output a class or class probability:

text
Tree 1 → P(relevant) = 0.9
Tree 2 → P(relevant) = 0.8
Tree 3 → P(relevant) = 0.3
Tree 4 → P(relevant) = 0.7
Tree 5 → P(relevant) = 0.4

The forest averages the probabilities (or majority-votes the hard labels):

P(relevant)=0.9+0.8+0.3+0.7+0.45=0.62P(\text{relevant}) = \frac{0.9+0.8+0.3+0.7+0.4}{5} = 0.62

So the forest predicts relevant.

For regression, every tree directly outputs a number, and the forest averages them:

y^=4.2+5.1+3.9+4.74\hat y = \frac{4.2+5.1+3.9+4.7}{4}

Why many trees

500 identical trees on identical data would be redundant. Random forests make each tree different in two ways.

First, each tree gets a bootstrap sample of the training data, randomly sampled rows with replacement:

text
Original:        A B C D E
Tree 1 training: A A C D E
Tree 2 training: B B C E E
Tree 3 training: A B B D E

Second, at each split, the tree only considers a random subset of features. If you have 100 features, a node might only be allowed to consider 10 randomly selected ones.

That creates a diverse population of trees. The intuition:

many noisy, somewhat independent models→average them\boxed{\text{many noisy, somewhat independent models}} \rightarrow \boxed{\text{average them}}

which reduces variance and overfitting.

Random forest vs XGBoost

Random ForestXGBoost
Trees trainedIndependentlySequentially
Main ideaAverage many diverse treesEach new tree corrects previous errors
Final predictionAverage / voteSum
Parallelizable trainingVery easilyLess so
TreesUsually deeperUsually shallower
Bias/variance intuitionMainly reduces varianceMainly reduces bias while controlling variance

Random Forest:

build many independent trees→average\boxed{\text{build many independent trees} \rightarrow \text{average}}

Gradient Boosting / XGBoost:

build tree→find mistakes→build next tree to correct them→sum\boxed{\text{build tree} \rightarrow \text{find mistakes} \rightarrow \text{build next tree to correct them} \rightarrow \text{sum}}

Both ultimately map:

tabular features→prediction\text{tabular features} \rightarrow \text{prediction}

Both are workhorses for L1 reranking in a candidate retrieval system: cheap tree models over a handful of features (BM25 score, embedding similarity, recency) cutting ~1000 candidates to ~100 before the expensive cross-encoder.