Sub topic 6
Combining tree predictions by voting and averaging
This chapter assumes you know how a single decision tree predicts, what a bootstrap sample is, and how bagging trains each tree on a resampled copy of the data. Here we cover the last step of a forest: turning many tree outputs into one prediction.
Why aggregation reduces error
Each tree in a forest is a noisy predictor. Its errors come partly from the particular bootstrap sample it saw. If those errors are not perfectly correlated across trees, combining the trees cancels part of the noise.
For B predictors that each have variance σ² and average pairwise correlation ρ, the variance of their average is:
ρσ² + (1 − ρ)σ² / B
Adding trees shrinks the second term toward zero. The first term stays. This is why the next chapters focus on making trees less correlated, and why more trees help only up to a point.
Regression: averaging
For regression, the forest prediction is the mean of the tree predictions.
Classification: hard and soft voting
There are two ways to combine classification trees.
- Hard voting takes the class predicted by most trees (majority vote).
- Soft voting averages the class probabilities from each tree, then picks the class with the highest average.
A fully grown tree's leaf is often pure, so its probability is 0 or 1. With such trees both methods give similar results. With limited depth or min_samples_leaf, leaves hold mixed classes, and soft voting uses that extra information.
scikit-learn's RandomForestClassifier uses soft voting. Its predict_proba returns the mean of the trees' probabilities, and predict returns the class with the largest mean.
| Method | What each tree contributes | Output |
|---|---|---|
| Hard voting | A class label | Most common label |
| Soft voting | Class probabilities | Class with highest mean probability |
| Averaging (regression) | A number | Mean of the numbers |
Seeing the difference
The code below builds a small forest by hand, as in the earlier chapters, and compares the two voting rules.
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0, stratify=y
)
rng = np.random.default_rng(0)
trees = []
for i in range(25):
idx = rng.integers(0, len(X_train), size=len(X_train))
tree = DecisionTreeClassifier(max_features="sqrt", min_samples_leaf=5, random_state=i)
trees.append(tree.fit(X_train[idx], y_train[idx]))
hard = np.stack([t.predict(X_test) for t in trees]).mean(axis=0) > 0.5
soft = np.stack([t.predict_proba(X_test) for t in trees]).mean(axis=0).argmax(axis=1)
print("Hard voting accuracy:", (hard.astype(int) == y_test).mean())
print("Soft voting accuracy:", (soft == y_test).mean())
print("Rows where the two rules disagree:", (hard.astype(int) != soft).sum())Setting min_samples_leaf=5 makes leaves impure, so tree probabilities are fractions rather than 0 and 1. On most rows the two rules agree. They differ only on close calls, where one rule's tie-like result is resolved differently.
Common errors
- Averaging labels in multiclass problems. Averaging labels 0, 1, 2 is meaningless. Use a per-class vote count or average probabilities.
- Treating the mean probability as calibrated. Forest probabilities are often pulled away from 0 and 1, because averaging many trees rarely produces extreme values. Use them for ranking. Calibrate them (for example with
CalibratedClassifierCV) if you need true probabilities. - Ties in hard voting. With an even number of trees and two classes, a tie is possible. Using an odd number of trees avoids it, and soft voting rarely ties.
- Expecting averaging to fix bias. Averaging reduces variance. If every tree underfits in the same way, the forest does too.
Exercise
A forest of 4 trees gives, for one row and classes A and B, the probabilities of A: 0.9, 0.4, 0.4, 0.4. What do hard voting and soft voting predict?
Show answer
Hard voting: three trees have A below 0.5, so they vote B, and one votes A. The prediction is B. Soft voting: the mean probability of A is (0.9 + 0.4 + 0.4 + 0.4) / 4 = 0.525, so the prediction is A. The rules disagree because the one confident tree outweighs three weak ones under soft voting.
Exercise
In the variance formula ρσ² + (1 − ρ)σ² / B, what happens to the variance as B grows very large, and what does this imply about adding trees?
Show answer
The second term goes to zero and the variance approaches ρσ². Adding trees cannot reduce variance below that floor, so lowering the correlation ρ between trees is the only way to improve further.