Skip to content
Tutorials Center
My learning

Topics

Lesson 102 of 111 ยท Projects

Project: Sales and Demand Forecasting

Forecast daily sales with scikit-learn: build lag and calendar features, validate with time-ordered splits, avoid leakage, and compare baselines, direct and recursive forecasts.

Every shop, warehouse and web service has to answer the question "how much will we sell next week?" Order too little and you lose sales, order too much and goods expire. This project builds a daily sales forecast end to end: look at the data, set baselines, create lag and calendar features, validate in time order, and then forecast 28 days ahead in two different ways.

It applies the ideas from Machine Learning for Time Series to a more realistic table. The data is generated with a fixed seed (two years of daily sales) so that every number below matches what you get when you run it. The series has a rising level, a weekly pattern (weekends sell more), a summer peak, promotion days that lift sales by 25%, holidays that cut them by 35%, and some random noise. That mirrors real demand data, and since we know what we planted, we can check whether the models find it.

The forecasting workflow

Look Trend, weekdays, season, promos.
Baselines Beat the simple rules first.
Features Lags, rolling means, calendar.
Validate in order Train on the past only.

Then forecast and look at where the errors fall.

1 / 2

Step 1: Look at the data and set baselines

Print the table, then average by weekday and by promo flag. The weekday means range from about 230 (Monday) to 353 (Saturday), and promo days sell 336 on average against 271 on other days, although part of that gap is simply that promotions fall on random weekdays and the level grows over time.

Then compute baselines for the last 28 days, which is the forecast we will make on the day before the window starts. As in Baselines and Dummy Models, these rules cost nothing and tell you what a model has to beat. We use two error measures. MAE is the mean absolute error in sales units. WAPE (weighted absolute percentage error) is the total absolute error divided by total sales, which is easy to explain to a manager and does not blow up on small days the way MAPE does.

Python Explore the sales table and compute baselines

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 baselines show how much structure the series has. "Same as yesterday" and "average of the last 28 days" are off by 14-15%. Repeating the last four weeks by weekday (seasonal naive) cuts the error to 7.9%, and the weekday average over eight weeks, which smooths the noise but is slightly out of date because sales keep growing, gets 10.1%. The test window also contains Christmas Eve and Christmas Day, so every baseline will be wrong on those days. That is the kind of knowledge a model can use and a rule cannot.

Step 2: Lag and calendar features, validated in time order

Frame the one-day-ahead problem as regression (how this works). The features for a day may use only information that is available before it:

  • Lags: sales 1, 7, 14 and 28 days earlier.
  • Rolling statistics: the mean of the last 3, 7 and 28 days. Always compute them on the series shifted by one day. A rolling mean that includes today's value contains the thing you are trying to predict.
  • Calendar: day of week, month, day of year.
  • Known-in-advance flags: promotions and holidays are planned, so using them for tomorrow is legitimate. A flag that is only known after the fact (for example, "a stockout happened") is not.

For validation, shuffled k-fold is wrong, because it trains on the future and tests on the past. Use TimeSeriesSplit: each fold trains on everything before a block of days and tests on that block (Cross-Validation for Special Data).

Python Time-ordered CV: Ridge, gradient boosting and a leaky feature

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 simple linear model wins: Ridge has a mean error of 21.9 across the five folds, beating seasonal naive (31.5) in every fold. Histogram gradient boosting does worse (27.9) than the linear model even though it is the more flexible one. The reason is the rising level: sales grow steadily, and a tree model cannot extrapolate. It predicts averages of values it saw in training, so it lags behind a trend that continues into new territory. A linear model continues the trend. This is a good example of why you compare models of different families, and why a flexible model is not automatically the better forecaster. (Common fixes are to predict the change or the ratio to a recent level, or to model the trend separately.)

The last line is the warning. When the rolling mean includes today's value, Ridge's error falls to 18.3, a 16% "improvement" that comes from leakage, not skill. In production that feature would not exist at prediction time, and the score would drop back. If a time-series score looks too good, check whether any feature touches the present day.

Step 3: Forecast 28 days ahead

One-day-ahead accuracy is not what most planners need. They need the next 28 days, so you need a multi-step strategy. The two basic ones are:

  • Direct. Use only features you will know on the forecast origin: trend, weekday, season, planned promotions and holidays. Then every day of the horizon is predicted independently, and errors do not build on each other.
  • Recursive. Keep the one-day lag model, predict day 1, then use that prediction as the lag for day 2, and so on. This reuses a good one-step model but each error is fed back, so mistakes accumulate.

The example builds both and compares them on the last 28 days against the seasonal-naive baseline. The direct model is a regression of log(sales) on a time index, weekday dummies, yearly sine and cosine terms, and the two flags. Taking the log turns multiplicative effects (a 25% lift) into additive ones that a linear model handles well. The recursive model is a gradient boosting regressor on lag features.

Python 28-day forecast: direct calendar model versus recursive lag model

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 direct calendar model has a WAPE of 4.6% (MAE 12.2), a large improvement over seasonal naive (7.9%). The recursive model is worse than the baseline (10.4%), and the weekly breakdown shows why: its error grows from 19 in the first week to 45 in the fourth, as the fed-back predictions drift. On the two Christmas days the baseline predicts 243 and 249, when sales were 171 and 151, because it repeats the previous weeks. The calendar model, which knows about the holiday flag, predicts 154 and 158, close to the real values.

Be careful about the conclusion. The direct calendar model wins here largely because we generated the data from exactly such calendar effects, and in real data you would test it the same way before believing it. What carries over to real projects is the process: start with baselines, validate in time order, keep leaky features out, compare strategies for the full horizon and look at where the errors fall.

What to do next on real data

  • Add known-in-advance features: price, planned promotions, weather forecasts, school holidays. Features you will only know later (actual weather) must be replaced by forecasts.
  • Forecast many series. A single global model across stores and products, with an identifier feature, often beats one model per series.
  • Report uncertainty. Planners need a range. Fit models for the 10th and 90th percentiles (for example with the quantile loss in gradient boosting), and check that about 80% of actuals land inside.
  • Backtest on several origins, not one 28-day window, and report the spread, like the per-fold errors above.
  • Monitor and retrain as patterns drift (monitoring).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why must rolling statistics be computed on the series shifted by one day?

    A rolling mean that includes today contains today's sales. In production that value is unknown at prediction time, so the model's cross-validated score would be inflated.
  2. 2. Why is TimeSeriesSplit preferred over shuffled k-fold for forecasting?

    Shuffled folds train on days that come after the test days and let the model peek into the future. TimeSeriesSplit keeps the time order, so the test mimics real use.
  3. 3. A gradient boosting model does worse than Ridge on a series with a steadily rising level. What is the most likely reason?

    A tree predicts averages of training targets, so it cannot follow a trend into new territory. A linear model continues the trend. Modeling the change or the ratio to a recent level helps trees.
  4. 4. In a recursive multi-step forecast, why does the error tend to grow with the horizon?

    From the second day on, the lag features contain predictions instead of real sales. Their errors propagate into every later step.

Summary

  • Start with baselines (naive, seasonal naive, weekday averages) and report MAE and WAPE.
  • Build lag, rolling (on shifted data), calendar and planned-event features, and validate with time-ordered splits.
  • A rolling mean that includes today's value is a leak; scores that are too good deserve suspicion.
  • Tree models cannot extrapolate a trend; linear models can, so compare model families.
  • For a multi-day horizon, a direct model on features known in advance often beats a recursive model whose errors accumulate.

Next: classify instead of forecast in Project: Heart Disease and Breast Cancer Diagnosis.