Tuning Random Forests: Trees, Depth and mtry
A random forest has fewer hyperparameters than gradient boosting, but the ones it has matter: the number of trees, how deep each tree is allowed to grow, and mtry — how many features are randomly considered at each split — control its bias-variance trade-off in distinct ways.
Prerequisites: Bagging and Variance Reduction
A random forest is a bagged ensemble of trees with one extra trick: at every split, instead of considering all features, it considers only a random subset of size . That addition, on top of bagging's bootstrap resampling, is what makes random forests behave differently from plain bagged trees, and it comes with its own hyperparameters. Compared to gradient boosting's long list of interacting knobs, a random forest has a short one: number of trees, depth, and .
The analogy: a committee forced to look away
Bagging alone is a committee where each member sees a slightly different slice of evidence but is free to weigh every argument. Random forests add a twist: at every decision point, each member is forced to consider only a random handful of arguments, ignoring the rest even if their favorite is excluded that time — forcing different members down different reasoning paths even on similar evidence.
The mechanics: three knobs, three different jobs
Number of trees (): unlike boosting, more trees essentially never overfits — each is grown independently, and averaging reduces variance monotonically, so is mostly a compute-versus-diminishing-returns call, flattening out somewhere between 100 and 1000. Depth / minimum leaf size: forests traditionally grow trees fully, relying on averaging across decorrelated trees to control variance, though capping depth can help on noisy data. : features randomly sampled per split, typically for classification or for regression. Smaller forces more diversity (lower ) at the cost of weaker individual trees, since good features get excluded more often; larger (up to , plain bagging) gives stronger individual trees but less diversity.
In plain English: directly controls the correlation term — shrinking it pushes down but can raise each tree's own variance if it's forced to ignore genuinely strong features too often; the sweet spot balances the two.
Worked example: sweeping mtry on a 20-feature dataset
Predicting bond downgrade with features: default gives out-of-bag error 12%. (plain bagging) raises it to 15% — every tree keeps choosing the same dominant features at the top split, making trees highly correlated. raises it to 17% the other way — trees are so restricted that many build genuinely weak splits, and higher individual error outweighs the diversity gain. The default region sits close to the actual sweet spot here, typical though still worth confirming with a small grid search.
Think of as sliding along the complexity axis above: too low pushes trees toward high individual bias (weak splits forced by restriction), too high pushes the ensemble toward high correlation — either way test error rises, with a minimum in between.
What this means in practice
Start from standard defaults (unpruned trees, a few hundred of them, ) and treat as the one hyperparameter most worth tuning via cross-validation or out-of-bag error, since it directly trades tree strength against diversity; tree count mostly just needs to be large enough that error has flattened.
Random forests have three hyperparameters with three jobs: more trees almost always helps up to a plateau, mainly a compute trade-off; controls the diversity-versus-strength trade-off and is the knob most worth tuning; depth mostly matters for noisy data where unrestrained trees would fit pure noise.
Unlike gradient boosting, adding more trees to a random forest does not cause overfitting — a common but incorrect worry when tuning. Overfitting comes from growing individual trees too deep on noisy data or setting too high, not from adding more trees.
Related concepts
Practice in interviews
Further reading
- Breiman, Random Forests (2001)
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning, ch. 15.3