Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 40 ยท Projects and Practice

Project: Sales Forecasting with Lag Features

Turn a daily sales series into a supervised table with lags, rolling means and calendar features, validate with a time split, and fix the trend problem by predicting a ratio.

Gradient boosting is not a time-series model, but it forecasts very well once you turn the series into a table: each row describes one day with features computed from the past (yesterday's sales, last week's sales, a recent average) and calendar information, and the target is that day's sales. This project builds such a table, validates it correctly with a time split, and meets the extrapolation problem from Pitfalls in a realistic setting.

Step 1: a synthetic sales series

The generated daily series has a weekly pattern, a yearly pattern, a steady upward trend, promotions and noise. That is enough to see why each feature is needed.

Python Step 1: generate and inspect the series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You can see a clear weekly pattern (the weekend is busier) and, in the last line, the trend: late sales are far higher than early sales. Keep that in mind; it will matter in Step 3.

Step 2: lag features and a time split

The rule for every feature: it must be known at the moment of the forecast. We forecast one day ahead, so yesterday's sales are known, but today's are not. Lags and rolling statistics are built with shift so that the current row never sees its own target. Rolling means are computed on the shifted series for the same reason.

Python Step 2: build the table and compare with a naive forecast

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The naive forecast, "the same weekday last week", is a strong baseline for seasonal data and you must beat it. In one run its MAE was 18.6 (about 7.6% error). The raw LightGBM model did worse, with an MAE of 23.9, and the cause shows in the last line: its average prediction in the test period (218) is lower than the actual average (239). The test days have higher sales than anything in the training period (the trend keeps rising), and trees cannot predict beyond the largest values they have seen.

Step 3: fix the trend by predicting a ratio

Instead of the sales level, predict sales relative to a recent level, for example the ratio of today's sales to the 28-day rolling mean. The target now has no trend, so it stays inside the range seen in training. Multiply the prediction back by the rolling mean to get sales. Lag features are also divided by the same level, so the model sees relative values.

Python Step 3: model the ratio to the recent level

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare with Step 2. In one run the average prediction (241) now matches the actual average (239), and the MAE falls to 12.2 (5.0% error), well below both the naive baseline (18.6) and the raw-level model (23.9). The importance shows the model leaning mostly on the weekday (dow, about two thirds of the gain) and the promotion flag (about 15%), which are exactly the real drivers. Predicting a ratio or a difference is the standard fix for trending series with boosted trees.

Step 4: forecasting several days ahead

One-step-ahead forecasts use real lags. For a 7-day horizon there are two strategies:

  • Recursive: predict day 1, feed that prediction as lag_1 for day 2, and so on. It needs one model, but errors accumulate.
  • Direct: train a separate model for each horizon h, using features known h days earlier (lags of at least h). It costs more models but does not accumulate error.

A good first approach is to train one direct model per horizon with a horizon feature, or to use only lags of 7 days and more so that a single model serves the whole week. Always evaluate with a rolling-origin backtest (several cutoffs, each training on the past and predicting the next block), because one test period can be lucky. sklearn.model_selection.TimeSeriesSplit creates these splits.

flowchart LR
  A["Series"] --> B["Features from the past only<br/>lags, rolling means"]
  B --> C["Target as ratio or difference<br/>(removes the trend)"]
  C --> D["Boosted trees"]
  D --> E["Multiply back to sales units"]
  E --> F["Compare with the naive forecast<br/>on a time-based test"]

A forecasting pipeline for boosted trees.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is the rolling mean computed on s.shift(1) instead of s?

    Using the day's own sales as a feature would leak the target; shifting keeps only past information.
  2. 2. The raw-level model predicts below the actual average in the test period. What is the main reason?

    Leaf values are constants from the training range; a rising trend pushes the targets beyond it.
  3. 3. What is the benefit of predicting the ratio to a recent level?

    A de-trended target keeps predictions inside the range the trees have seen.
  4. 4. Why use a rolling-origin backtest rather than one test period?

    A single test period can be easier or harder than average; multiple cutoffs average out the luck.

Summary

  • Turn a series into a table with lags, rolling statistics (always shifted) and calendar features.
  • Validate by time, and always compare with a naive seasonal baseline.
  • Trees cannot extrapolate trends: predict a ratio or difference to a recent level and convert back.
  • For multi-day horizons choose between recursive and direct strategies and backtest at several cutoffs.

Next: Practice Exercises and Cheat Sheet