Random Forest
An ensemble method combining multiple decision trees with feature randomness
Random Forest (RF) is an ensemble method that combines multiple decision trees to create a powerful, robust classifier. It extends bagging by adding feature randomness, forcing trees to be more diverse.
Despite being a complex ensemble model, a Random Forest is still made of simple, interpretable decision trees. Each tree follows clear if-then rules, making individual tree decisions easy to understand. The forest combines many such trees to achieve better predictions while maintaining the interpretability of its building blocks.
How Random Forest Works
Random Forest improves upon simple bagging by introducing randomness in both data and features:
The Problem with Simple Bagging: In bagging, every tree considers all features when deciding where to split. If some features are much stronger predictors, every tree picks those same features near the top. Trees become too similar, making the same types of errors, so averaging doesn't reduce variance as much as it could.
Random Forest's Solution: When a tree tries to split a node, it doesn't consider all features. Instead, it looks at a random subset of features and chooses the best split only from those. This forces each tree to explore different parts of the feature space, creating more diversity.
Step-by-step process:
- Take a random sample of the data (bootstrapping)
- Train a decision tree on this sample
- At each split, select a random subset of features and find the best split only among them
- Repeat to grow many trees, each on different data with different feature subsets
- Aggregate predictions: majority vote (classification) or average (regression)
Result: A collection of trees that are individually imperfect but collectively powerful. Each tree might overfit its sample, but when hundreds of trees are averaged, overfitting disappears. The model maintains low bias (deep, flexible trees) with low variance (averaged predictions).
Random Forests reduce variance by averaging many overfitting trees. A single tree overfits the training data, but when many diverse trees are averaged, their individual errors cancel out, producing stable predictions on unseen data.
Out-of-Bag (OOB) Error
Random Forests can estimate their own performance without a separate validation set using out-of-bag (OOB) samples.
When each tree is built with bootstrap sampling, about one-third of data points are left out. These left-out points serve as a test set for that tree. Since every data point is left out of some trees, we can use those trees to make predictions for it and calculate overall accuracy.
Advantages:
- No separate validation set needed
- Efficient use of data
- Fast performance estimation with almost no extra cost
In practice: Set oob_score=True in scikit-learn. The model reports the OOB score after training, giving a quick estimate of generalization performance.
This visualization shows how OOB error changes as more trees are added to the forest. Error decreases rapidly at first as variance reduces through averaging. Eventually, performance plateaus—adding more trees provides diminishing returns. This helps determine the optimal number of trees: enough to minimize error, but not so many that training becomes unnecessarily expensive.
Key Hyperparameters
Random Forests work well with defaults, but understanding parameters helps optimize performance. Hyperparameters fall into three categories: forest-level controls that affect the ensemble behavior, tree-level controls that shape individual trees, and utility parameters for reproducibility and performance.
Forest-Level Parameters
The n_estimators parameter sets the number of trees in the forest. More trees generally lead to better performance up to a point, as averaging over more predictions reduces variance. Values between 100-500 are common starting points, and you can increase this number until performance plateaus—the point where adding more trees provides diminishing returns.
max_features determines how many features each tree considers when looking for the best split at each node. For classification tasks, the default is the square root of total features, while regression typically uses one-third of total features. Choosing fewer features increases diversity among trees by forcing them to explore different parts of the feature space, which reduces correlation and variance. However, if you choose too few features, the model might miss important splits and lose accuracy.
The bootstrap parameter controls whether sampling is done with replacement when building each tree. The default is True, meaning each tree trains on a random sample with possible duplicates. Setting it to False makes each tree see the entire dataset, which removes one source of randomness. Keep it True for best generalization.
Setting oob_score to True tells the model to calculate out-of-bag error during training, providing a built-in validation estimate without needing a separate validation set. This is False by default but is highly useful for quick performance estimates.
Tree-Level Parameters
The max_depth parameter controls how deep each decision tree can grow. Deeper trees capture more complex patterns but are more prone to overfitting. By default (None), trees grow until all leaves are pure or contain too few samples to split further. Limiting max_depth is one of the simplest ways to prevent overfitting.
min_samples_split sets the minimum number of samples required to split a node. Higher values result in fewer splits and simpler trees, reducing sensitivity to noise in the training data. Similarly, min_samples_leaf ensures that leaf nodes contain a minimum number of samples, which smooths predictions and is particularly useful for regression problems.
Utility Parameters
The random_state parameter fixes the random seed to ensure reproducibility. By setting this to an integer value, your Random Forest produces the same results every time you train it on the same data—essential for debugging and comparing models.
n_jobs controls how many CPU cores to use during training. Setting it to -1 uses all available cores, which can significantly speed up training when building many trees since tree construction can be parallelized.
Hyperparameter Tuning
Grid Search: Define values for each parameter and test all combinations. It is thorough but expensive. Works best when parameter ranges are known.
Random Search: Randomly sample parameter combinations and faster than Grid Search. Finds good configurations early. Works well when not all parameters matter equally
Bayesian Optimization: Tools: Optuna, Hyperopt, Scikit-Optimize. Learns from previous results. Focuses search on promising regions. Best for expensive training or many parameters
The curved contour lines show levels of equal model error across the hyperparameter space. Moving toward the center of these contours means better-performing parameter combinations, while moving outward leads to higher error. This visualization helps explain why Bayesian optimization concentrates its search in promising regions instead of sampling blindly.
Practical approach: Start with Random Search for broad exploration, then refine with Grid Search around top performers. Use OOB error or cross-validation for evaluation.
Feature Importance
Random Forests provide two ways to measure feature importance:
Impurity-Based Importance (Gini Importance)
Calculated automatically during training. When a feature splits a node, measure how much it reduces impurity (Gini, entropy, or variance). Average this reduction across all trees.
Advantages: Very fast (computed during training) & Built into the model
Limitations: Favors features with many unique values, can favor continuous over categorical features & interpret as relative indicators, not absolute truth
Permutation Importance
Model-agnostic approach. Randomly shuffle values of one feature and measure performance drop.
- Large drop → feature was important
- Small/no drop → feature contributed little
Advantages: More robust measure, less biased toward high-cardinality features & works with any model
Limitations: Slower (requires multiple model evaluations)
Usually you use both methods. Impurity-based for quick insights, permutation importance for robust validation.
When to Use Random Forest
Random Forest is one of the most practical and widely-used algorithms for tabular data, largely because it works well with default parameters and requires minimal tuning to achieve strong performance. The algorithm handles both numerical and categorical features naturally and is robust to outliers and noise in the data, making it forgiving of imperfect data preprocessing. With enough trees in the forest, Random Forest has a low risk of overfitting, and because tree construction can be parallelized, training is often fast even on large datasets. Additionally, Random Forest provides feature importance scores, offering insight into which variables drive predictions.
Random Forest excels in both classification and regression tasks and serves as an excellent baseline model before exploring more complex approaches. It's particularly valuable when interpretability matters—while individual predictions come from many trees, feature importance helps explain which variables influence the model most. Random Forest works well with tabular data of any size, from small datasets to large-scale applications.
However, Random Forest has some limitations. At prediction time, it can be slower than single decision trees because it must query many trees and aggregate their results. While the forest as a whole is less interpretable than a single decision tree, this trade-off usually favors accuracy over simplicity. The model is also memory-intensive since it stores many complete trees, which can be a concern in resource-constrained environments. Finally, Random Forest can struggle with very high-dimensional sparse data, where other methods like linear models or gradient boosting might perform better.
Evaluation Metrics
For evaluation metrics, see:
- Classification tasks: Classification Evaluation Metrics
- Regression tasks: Regression Evaluation Metrics