The Data Leakage You Don't See: ML in Industrial Time Series
A true story from a Machine Learning project on plant data: a copper flotation rougher circuit with 7 cells, 34,319 records every 10 minutes throughout 2024. The goal was to predict Cu recovery from operational variables.
The first iteration of the model achieved R² = 0.64 with Random Forest. Decent numbers, apparently everything in order. Until I started questioning something: how was that R² evaluated?
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
That random train_test_split is responsible for the problem. In industrial time series, never split data randomly.
The problem with random split
Imagine we train with the 10:00 minute mark of a shift and test with the 10:10 minute mark of the same shift. The physical process between those two minutes hardly changed at all — same feed grade, same pH, same dosing. The model "sees" the response at the next instant almost identical to what it has in its training example.
The result: the model learns to memorize the temporal context, not generalize to new conditions. But generalizing to new conditions is precisely what you need when you deploy the model in production, where tomorrow is always a dataset the model has never seen.
This is known as temporal data leakage and is one of the most common — and most expensive — errors in ML applied to industrial processes.
The fix: chronological split
The rule is simple: old data trains, new data tests.
# Sort by date
df = df.sort_values('Fecha').reset_index(drop=True)
# Chronological split: 80% oldest / 20% most recent
cutoff = int(0.8 * len(df))
X_train, X_test = X.iloc[:cutoff], X.iloc[cutoff:]
y_train, y_test = y.iloc[:cutoff], y.iloc[cutoff:]
And for cross-validation within the train, use TimeSeriesSplit — which generates folds expanding forward, never "looking into the future":
from sklearn.model_selection import TimeSeriesSplit
tscv = TimeSeriesSplit(n_splits=5)
for train_idx, val_idx in tscv.split(X_train):
# fold 1: train[0:n/5], val[n/5:2n/5]
# fold 2: train[0:2n/5], val[2n/5:3n/5]
# ...
...

What happened to the R²
With the correct chronological split:
| Validation | R² (out-of-time) |
|---|---|
Random train_test_split |
0.64 ← inflated |
Chronological split + TimeSeriesSplit |
0.15 ← honest |
The honest R² is four times lower. That 0.64 was literally statistical fiction — the model was not going to work in the plant.
The mathematical balance behind the metric
R² measures how much better your model is than simply predicting the mean:
With \(R^2 = 0.15\), the model explains only 15% of the variance. In practice, it is often almost the same as predicting the historical mean — something a simple hand-crafted rule can do.
Why the "real" model is so poor
Three common hypotheses for plant models with honest low R²:
- Really weak signal in available features. Perhaps recovery depends heavily on variables that are not measured: ore mineralogy, pulp temperature, wear on cell impellers, water quality.
- Distribution changed between train and test. The plant may have had operational changes, maintenance, setpoint adjustments, different ore campaigns. The model trained on the past no longer applies.
- Long-term dynamics not captured with minute-level lags. A shift lasts 8–12 hours; the effects of reagent dosing can take shifts to stabilize.
The lesson
It is not "stop using Random Forest" or "your data is bad." The lesson runs deeper:
Incorrect validation gives you pretty but useless numbers. Before celebrating a high R², ask yourself: how was the split constructed? If there is any temporal or spatial component in your data (and there almost always is), random split is suspicious.
Another practical angle: if switching to correct validation causes your model to collapse, it was a mirage. Better to discover it in your notebook than in production, when a project is already committed to a client or boss.
Checklist before publishing an ML result
- [ ] Does my split respect the temporal/spatial structure of the data?
- [ ] Did I use
TimeSeriesSplit/GroupKFoldas appropriate? - [ ] Does my train contain information that won't be available at prediction time?
- [ ] Did I compare against a simple baseline (mean, persistence, linear regression)?
- [ ] Did I report the R² of the out-of-time test, not the optimistic cross-validation R²?
If you answered "no" to any of these, your number is inflated. Better to know now.