
Why My Random Forest Model Couldn't Predict Next Year's Temperature (And the Fix)
"A Random Forest temperature model that worked great for 1950-2024 completely flatlined on future years — because tree-based models can't extrapolate. Here's the hybrid fix: Random Forest within range, per-country linear trends beyond it."
I built a global temperature prediction app covering 196 countries from 1950-2024 using Random Forest regression. It worked great — until I asked it to predict 2025. Then it just... flatlined. Here's why, and how I fixed it.
The setup
A Random Forest trained on historical temperature data (country, year, climate data) to predict mean temperature. Test performance was solid: R² = 0.7332, MAE = 3.15°C, RMSE = 4.24°C.
A Random Forest trained on historical temperature data (country, year, climate data) to predict mean temperature. Test performance was solid: R² = 0.7332, MAE = 3.15°C, RMSE = 4.24°C.
The problem nobody warns you about
Random Forests — and tree-based models in general — cannot extrapolate beyond the range of their training data. A tree learns to split data into regions and predict the average value within each region. If every training example falls between 1950 and 2024, the model has literally never seen what "year = 2030" should look like — it just predicts whatever its nearest learned region says, which means future years get flattened to roughly the same value as the most recent training years. Not wrong code — just the wrong tool for the job past the training boundary.
Random Forests — and tree-based models in general — cannot extrapolate beyond the range of their training data. A tree learns to split data into regions and predict the average value within each region. If every training example falls between 1950 and 2024, the model has literally never seen what "year = 2030" should look like — it just predicts whatever its nearest learned region says, which means future years get flattened to roughly the same value as the most recent training years. Not wrong code — just the wrong tool for the job past the training boundary.
The fix: a hybrid model
Rather than switching models entirely, I split the problem:
Rather than switching models entirely, I split the problem:
- For years ≤ 2024 (within training range): keep the Random Forest — it's accurate and well-validated here
- For years > 2024 (extrapolation): switch to a per-country linear trend, fit on each country's own historical trajectory, which actually captures directional change over time instead of flattening it
This hybrid approach means predictions stay accurate where the model has real data to learn from, and switch to a method actually designed for extrapolation once you step outside that range.
A smaller but equally real lesson: GitHub's 100MB limit
My first trained model (200 trees) produced a corrupted, unusable file — turns out it silently exceeded GitHub's 100MB file size limit, which left a broken ~993-byte stub in the repo instead of a clear error. Retraining with fewer, shallower trees (
My first trained model (200 trees) produced a corrupted, unusable file — turns out it silently exceeded GitHub's 100MB file size limit, which left a broken ~993-byte stub in the repo instead of a clear error. Retraining with fewer, shallower trees (
n_estimators=50, max_depth=12) brought it down to a healthy ~8.3MB with barely any accuracy tradeoff.What it looks like now
The deployed app has three tabs: a direct Predict tab, an interactive World Globe visualization, and a Country Trend view showing each country's trajectory — including the extrapolated future years, clearly distinguished from the validated historical range.
The deployed app has three tabs: a direct Predict tab, an interactive World Globe visualization, and a Country Trend view showing each country's trajectory — including the extrapolated future years, clearly distinguished from the validated historical range.
Try it:
🔗 Code: github.com/Ansh-san/temperature-prediction-project
🔗 Code: github.com/Ansh-san/temperature-prediction-project
Has anyone else gotten tripped up by tree-based models silently flattening predictions outside their training range? Curious how others have handled extrapolation — hybrid approach, a different model entirely, or something else?
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article