Reframe time series for a tree model
turning forecasting into supervised learning.
lag and rolling-window features, calendar and cyclical encodings, then split chronologically to avoid leakage.
random shuffling that lets future data leak into training.
WHAT THIS TESTS The question checks whether you understand that gradient boosting trees have no built-in notion of time, so you must hand-craft temporal structure into features and respect ordering when validating.
A GOOD ANSWER COVERS Reframe forecasting as supervised regression where each row is a point in time, the target is the future value you want to predict, and the predictors are derived from the past. Core features include lagged values such as the value one step and one week ago, rolling aggregates like a seven-day moving average or standard deviation, and calendar attributes like day of week, month, and holiday flags. Encode cyclical features such as hour or month with sine and cosine pairs so the model treats December and January as adjacent. Add exogenous drivers like promotions or weather when available. Split chronologically, train on the past and test on the future, and use forward-chaining cross-validation.
COMMON WRONG ANSWERS Randomly shuffling and splitting rows, which lets future information train the model; computing rolling windows or scaling over the whole dataset before splitting, leaking statistics; or assuming a tree can extrapolate a rising trend beyond the range it saw during training.
LIKELY FOLLOW-UPS How do trees handle trend, and how would you detrend or difference the series first? How do you forecast multiple steps ahead, recursive versus direct? How do you prevent leakage in feature computation?
ONE CONCRETE EXAMPLE For daily sales, build a row per day with features sales_lag_1, sales_lag_7, a 7-day rolling mean, day-of-week, month, and a holiday flag, with the target being next-day sales. Train on January through October and test on November and December. Because trees cannot extrapolate, you difference the series to remove the upward trend so the model predicts changes rather than ever-larger absolute values.
Read the original → scikit-learn.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.