rules/mapie/shuffled-time-series-split.md

A shuffled split puts test rows between training rows in time, so a TimeSeriesRegressor looks better on them than on later rows. Pass shuffle=False.

diff
1-X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
2+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, shuffle=False)
3 series = TimeSeriesRegressor(model, cv=blocks)
4 series.fit(X_train, y_train)

What the rule reports

The rule reports train_test_split and train_conformalize_test_split without shuffle=False, where the first result goes into fit of a TimeSeriesRegressor.

How to fix it

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The unsafe fix adds shuffle=False at the end of the arguments and drops the comments between them. The parts then hold other rows. A split with stratify has no fix, because scikit-learn refuses stratify with shuffle=False.

On a random walk of 900 rows, the intervals covered the following shares of the test rows:

ModelTest rowsCoverage
TimeSeriesRegressora shuffled test part0.921
TimeSeriesRegressora test part after the training rows0.536
SplitConformalRegressor on a shuffled splitits own test rows0.891
SplitConformalRegressor on a shuffled splitthe 300 rows that came after them0.144

The rule can't see that the rows are in time order, so it is a warning.