Sub topic 13
Limitations and when to choose something else
This chapter assumes you can train and evaluate a forest in scikit-learn and know the roles of bagging, feature subsampling, and out-of-bag (OOB) scores. Random forests are a strong default for tabular data, but they have limits you should know before you rely on them.
Where forests fall short
They cannot extrapolate
A tree predicts the average of training targets in a leaf. A forest averages those leaf values, so its output always stays inside the range of the training targets. If the target keeps growing beyond the training data, the forest flattens out.
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
X = np.arange(0, 10, 0.1).reshape(-1, 1)
y = 2 * X.ravel()
forest = RandomForestRegressor(n_estimators=100, random_state=0).fit(X, y)
linear = LinearRegression().fit(X, y)
X_new = np.array([[15.0], [20.0]])
print("forest:", forest.predict(X_new))
print("linear:", linear.predict(X_new))The linear model returns 30 and 40. The forest returns about 19.8 for both inputs, because 19.8 is the largest target it saw. This matters for time series with trends, prices, and any problem where test inputs fall outside the training range.
They struggle with sparse, high-dimensional data
With text represented as thousands of sparse columns, most features are useless for any given split. Random feature subsets then often contain no useful feature, and the trees are weak. Linear models usually do better and train faster there.
They are large and slow at prediction time
A forest stores hundreds of deep trees. The model can take hundreds of megabytes, and prediction cost grows with the number of trees and their depth. This can break latency or memory limits on embedded or real-time systems.
They are hard to interpret
One tree can be read as a set of rules. A forest of 300 trees cannot. Feature importances show which inputs matter, but not how they affect a single prediction.
They tend to give rough probabilities and coarse boundaries
Probabilities from predict_proba are vote fractions. They are often poorly calibrated, and they are usually pushed away from 0 and 1. Because splits are axis-aligned, a diagonal boundary needs many small steps to approximate.
Choosing an alternative
| Situation | Better choice | Reason |
|---|---|---|
| Need the best accuracy on tabular data | Gradient boosting (for example `HistGradientBoostingClassifier`) | Reduces bias, not only variance |
| Target trends beyond training range | Linear model or a model with a trend term | Can extrapolate |
| Sparse text features | Logistic regression or linear SVM | Handles many weak features |
| Images, audio, raw text | Neural networks | Learn features from raw input |
| Must explain each decision | Single shallow tree or linear model | Directly readable |
| Tight latency or memory budget | Fewer, shallower trees, or a linear model | Smaller and faster |
Practical advice
Use a forest as a baseline. It needs little tuning, resists overfitting, and gives you an OOB score for free. If a simpler model matches it, prefer the simpler model. If boosting beats it by a clear margin on cross-validation, the gain is usually real.
Exercise
You train a random forest regressor on house prices from 2015 to 2020 and use it to predict prices in 2024, when the market is much higher. What goes wrong, and what could you do?
Show answer
The forest cannot predict above the highest prices it saw in training, so it underestimates 2024 prices. Options include modelling a trend with a linear model and fitting the forest to the residuals, predicting price changes instead of price levels, or using a model that can extrapolate.