๐Ÿ“ˆ Machine Learning ยท Lecture 19 of 47

Random Forests: Decorrelated Trees and Robust Predictions

Random forests add feature randomness to bagged trees, breaking their correlation. We explain the algorithm, its hyperparameters, OOB estimates, feature importance pitfalls and when forests are the right tool.

If you can use only one algorithm for a new tabular dataset with no time to tune, the random forest is an excellent choice. Introduced by Leo Breiman in 2001, it combines bagging with an extra dose of randomness, producing models that are accurate, robust to noise and outliers, and nearly impossible to badly misconfigure.

The key idea: decorrelate the trees#

Bagged trees are correlated because they all tend to pick the same strong features near the root. Recall the ensemble variance formula:

$$ \text{Var} = \rho\,\sigma^2 + \frac{1 - \rho}{B}\,\sigma^2 $$

To reduce $\rho$, random forests restrict each split to a random subset of $m$ features (out of $d$). Strong features are unavailable at some splits, forcing trees to explore other structure. Each tree becomes slightly worse individually, but the ensemble improves because errors are less correlated.

The algorithm#

For $b = 1, \dots, B$:

  1. Draw a bootstrap sample of the training data.
  2. Grow a tree on it. At each node:
    • select $m$ features at random;
    • find the best split among only those $m$ features;
    • split, and recurse โ€” typically growing trees deep, with no pruning.

Predict by averaging (regression) or majority vote/averaged probabilities (classification).

Typical defaults: $m = \sqrt{d}$ for classification, $m = d/3$ (or all features, in some libraries) for regression.

Hyperparameters that matter#

HyperparameterEffectGuidance
n_estimators ($B$)More trees โ†’ lower variance; never overfits200โ€“1000; stop when OOB error plateaus
max_features ($m$)Smaller โ†’ more decorrelation, weaker treesTune among $\sqrt{d}$, $\log_2 d$, 0.3โ€“0.5ยท$d$
min_samples_leafLarger โ†’ smoother, less variance1โ€“10; larger for noisy regression
max_depthLimit tree depthUsually unlimited; limit for speed/memory
class_weightReweight classes"balanced" for imbalanced data

Random forests are famous for working well with defaults โ€” a big practical advantage.

python
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_absolute_error

X, y = fetch_california_housing(return_X_y=True, as_frame=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=0)

tree = DecisionTreeRegressor(random_state=0).fit(X_tr, y_tr)
print("single tree MAE:", round(mean_absolute_error(y_te, tree.predict(X_te)), 3))

for m in [1.0, 0.5, 0.33]:
    rf = RandomForestRegressor(n_estimators=300, max_features=m, oob_score=True,
                               n_jobs=-1, random_state=0).fit(X_tr, y_tr)
    print(f"RF max_features={m}: test MAE {mean_absolute_error(y_te, rf.predict(X_te)):.3f}"
          f"  OOB R^2 {rf.oob_score_:.3f}")

(With max_features=1.0, the forest is plain bagging.)

Feature importance โ€” handle with care#

Random forests offer two importance measures:

  1. Mean decrease in impurity (MDI) โ€” fast, computed during training, but biased towards continuous and high-cardinality features and computed on training data.
  2. Permutation importance โ€” shuffle one feature in validation data and measure the drop in performance. More reliable, but can understate the importance of correlated features (the model uses the correlated partner instead).
python
from sklearn.inspection import permutation_importance
rf = RandomForestRegressor(n_estimators=300, n_jobs=-1, random_state=0).fit(X_tr, y_tr)
pi = permutation_importance(rf, X_te, y_te, n_repeats=10, random_state=0, n_jobs=-1)
for i in pi.importances_mean.argsort()[::-1]:
    print(f"{X.columns[i]:<12} {pi.importances_mean[i]:.3f} ยฑ {pi.importances_std[i]:.3f}")

Other useful by-products#

  • OOB error โ€” free validation.
  • Proximity matrix โ€” how often two examples land in the same leaf; useful for clustering, outlier detection and imputing missing values.
  • Quantile regression forests โ€” keep all leaf targets to estimate prediction intervals.
  • Isolation Forest โ€” a related randomised-tree method for anomaly detection.

Strengths and weaknesses#

Strengths: strong accuracy with little tuning; robust to outliers and irrelevant features; handles non-linearity and interactions; parallelisable; no feature scaling; OOB estimates.

Weaknesses: less interpretable than a single tree; large memory footprint and slower prediction than a linear model; cannot extrapolate beyond the training target range; usually slightly less accurate than well-tuned gradient boosting on tabular data.

Random forest or gradient boosting?#

ConsiderationRandom forestGradient boosting
Tuning effortLowModerate to high
Peak accuracy on tabular dataVery goodOften best
Overfitting by adding treesNoYes (needs early stopping)
Training parallelismAcross treesWithin trees
Noisy labelsVery robustCan chase noise
JA
Written by

Janin A Apurba

B.Sc. in CSE, AUST ยท Advanced ICT Officer, CNRS-UNHCR. Teaching AI, ML and Deep Learning to the next generation of engineers and researchers.

Keep learning

Related lectures

๐Ÿ“ˆ Machine Learning

Bagging and the Bootstrap: Variance Reduction by Averaging

Averaging many noisy models trained on resampled data produces one stable model. We study the bootstrap, derive why bagging reduces variance, and use out-of-bag error as a free validation estimate.

Intermediateโฑ 5 min#067
๐Ÿ“ˆ Machine Learning

Boosting I: AdaBoost and the Power of Weak Learners

Can many weak rules of thumb combine into a strong classifier? AdaBoost answered yes. We walk through the reweighting algorithm, derive it as exponential-loss minimisation, and discuss its margins and sensitivity to noise.

Intermediateโฑ 5 min#069
๐Ÿ“ˆ Machine Learning

Boosting II: Gradient Boosting Machines

Gradient boosting performs gradient descent in function space, fitting each new tree to the negative gradient of the loss. We derive the algorithm, explain shrinkage and subsampling, and tune it properly.

Advancedโฑ 5 min#070