Lesson 5 · Real-World Data Analytics
Sales Forecasting in Python: Holt-Winters vs Baselines | Data Analytics #5
Can you trust your sales forecast? We build a weekly revenue forecast for a real retailer in Python, compare it honestly against naive, moving-average and…
- CourseReal-World Data Analytics
- Lesson5 of 100
- FormatJupyter notebook · 30 code cells
- Data1 dataset
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
- online_retail_clean.csv preview36.2 MB
📓 Full notebook
Download .ipynbData Analytics 100, Video 5: Sales Forecasting for Retail Demand#
- Video five of the hundred-video real-world data analytics series.
- Building a real weekly revenue forecast, tested honestly against real held-out weeks the model never saw.
- Let's get into it.
This lesson is for learners who want to build a forecast they can actually defend, not just a line that looks plausible. The business question is: how much revenue should this retailer expect over the coming weeks, and how far can that forecast be trusted? You will work with the cleaned UK online gift retailer data from Video 1, online_retail_clean.csv, using InvoiceDate and Revenue to build a weekly revenue series from December 2010 to December 2011.
The key idea is an honest test. You hide the most recent weeks, build forecasts using only the earlier data, and then measure how wrong each forecast was. Simple baselines set the bar that any smarter model must beat.
In this lesson you will:
- Turn transactions into a complete daily series, then into whole weeks with
resample. - Split the series into training and test weeks and write MAE and RMSE error functions.
- Compare naive, moving-average, linear-trend and Holt exponential smoothing forecasts using
statsmodels. - Run a rolling backtest, then refit on all data and save a six-week forecast.
Prerequisites: you should be comfortable with pandas datetime columns and groupby. No prior statistics is assumed.
Part 1: Why Forecasting Needs a Real Honest Test#
A forecast is only useful if you know how wrong it is likely to be. The only fair way to find out is to test it on data the model has never seen, just as it will face the real future. This step imports pandas, numpy for maths on arrays and matplotlib for charts, then loads the cleaned file with parse_dates=['InvoiceDate']. The output (391150, 9) confirms you are using the same cleaned data as the earlier lessons.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
clean = pd.read_csv('online_retail_clean.csv', parse_dates=['InvoiceDate'])
clean.shape
Part 2: Real Daily Revenue Series#
Forecasting needs one value per time period, so you first collapse hundreds of thousands of transactions into daily totals. .dt.date strips the time from each timestamp, and groupby('DateOnly')['Revenue'].sum() adds up each day's revenue. pd.to_datetime turns the index back into proper dates so time-based tools work. The output shows 305 days with sales. As the data spans just over a year, some calendar days must be missing, which the next step deals with.
clean['DateOnly'] = clean['InvoiceDate'].dt.date
daily = clean.groupby('DateOnly')['Revenue'].sum()
daily.index = pd.to_datetime(daily.index)
daily.shape[0]
Part 3: Filling Real Gaps with Zero-Revenue Days#
Days with no sales are not missing data: they are real days when revenue was zero. Dropping them would squeeze the timeline and make weekly totals wrong. pd.date_range(..., freq='D') creates every calendar day from first to last, and reindex(full_range, fill_value=0) adds the missing days with zero revenue. The output shows the series now has 374 days, so 69 days had no sales at all. Common mistake: using fillna(0) alone does nothing here, because the missing days are not in the index yet.
full_range = pd.date_range(daily.index.min(), daily.index.max(), freq='D')
daily = daily.reindex(full_range, fill_value=0)
daily.shape[0]
Part 4: Real Day-of-Week Pattern#
Weekly patterns explain a lot of day-to-day noise. daily.index.dayofweek gives a number for each date, where 0 is Monday and 6 is Sunday, and groupby(...).mean() averages revenue per weekday. Thursday (3) is strongest at about 35,912 per day, and Tuesday (1) is next at 31,556. Sunday (6) averages 14,712, and Saturday (5) is exactly 0. Note that these averages include zero-filled holiday days, so they are slightly lower than averages over trading days only.
dow_avg = daily.groupby(daily.index.dayofweek).mean()
dow_avg.round(0)
Part 5: Confirming the Real Saturday Gap#
A weekday with zero average revenue is unusual, so you confirm it rather than assume it. The code reads the Saturday value with dow_avg[5] and checks whether it equals 0. The output is True: the business took no orders on any Saturday in the data. This matters for forecasting, because a daily model would need to know Saturdays are always zero. Moving to weekly totals, as the next step does, avoids the problem entirely.
saturday_avg = dow_avg[5]
saturday_avg == 0
Part 6: Aggregating to Real Weekly Revenue#
Daily revenue is very noisy, with zero Saturdays and holiday gaps. Weekly totals are smoother and closer to how a retailer plans stock and staff. resample('W') groups the daily series into weeks ending on Sunday, and .sum() adds up each week. The output shows 54 weeks. The first and last of these are likely incomplete, because the data starts on a Wednesday (1 December 2010) and ends on a Friday (9 December 2011), which is handled next.
weekly = daily.resample('W').sum()
weekly.shape[0]
Part 7: Trimming Real Partial Weeks#
A partial week has fewer trading days, so its total looks like a sudden drop that never really happened. Leaving it in would mislead any model. weekly.iloc[1:-1] removes the first and last week. The length line is not displayed, but this leaves 52 full weeks. The output shows the first three: the week ending 12 December 2010 at 210,678.74, then 161,598.80, then 45,285.91 for the week including Christmas. That Christmas dip is real and shows how seasonal this business is.
weekly = weekly.iloc[1:-1]
weekly.shape[0]
weekly.head(3)
Part 8: Visualizing the Real Weekly Revenue History#
Plotting the history before modelling shows you the trend, seasonality and odd points a model must cope with. The code draws weekly revenue as a line with a dot per week, rotates date labels by 45 degrees and saves the chart to weekly_revenue_history.png. Open the image and look for a fairly flat first half of 2011, then a clear rise from September into November, the pre-Christmas peak. That strong late climb is exactly what makes the final weeks hard to forecast.
plt.figure(figsize=(11, 5))
plt.plot(weekly.index, weekly.values, marker='o', color='navy')
plt.xlabel('Real Week')
plt.ylabel('Real Weekly Revenue (GBP)')
plt.title('Real Weekly Retail Revenue History')
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig('weekly_revenue_history.png', dpi=120)
plt.close()
Part 9: The Real Train/Test Split#
The honest test starts here. weekly.iloc[:-8] keeps all but the last eight weeks for training, and weekly.iloc[-8:] holds back those final eight for testing. The output (44, 8) confirms the split. With time series you always split by time, never randomly, because a random split would let the model learn from weeks that come after the ones it is predicting. Note that the test weeks run from mid-October to early December, the busiest part of the year, so this is a tough test.
train = weekly.iloc[:-8]
test = weekly.iloc[-8:]
len(train), len(test)
Part 10: Real Error Metric Helpers#
You need a consistent way to score each forecast. MAE (mean absolute error) is the average size of the miss in pounds, ignoring whether it was over or under. RMSE (root mean squared error) squares each miss before averaging and then takes the square root, so large misses are punished more. np.asarray makes sure both inputs are plain arrays, so pandas index alignment cannot cause surprises. Defining the functions produces no output. Lower is better for both.
def mae(actual, forecast):
return np.mean(np.abs(np.asarray(actual) - np.asarray(forecast)))
def rmse(actual, forecast):
return np.sqrt(np.mean((np.asarray(actual) - np.asarray(forecast)) ** 2))
Part 11: Real Baseline 1, the Naive Forecast#
The naive forecast is the simplest possible model: next week will look like the last week you saw. It sets the minimum standard; a model that cannot beat it is not worth using. The code repeats the last training value eight times as a Series indexed by the test weeks. The output shows an MAE of about 47,079 and an RMSE of about 51,439 pounds per week. Those errors are large because the last training week came before the steep pre-Christmas rise.
naive_forecast = pd.Series([train.iloc[-1]] * len(test), index=test.index)
naive_mae, naive_rmse = mae(test, naive_forecast), rmse(test, naive_forecast)
round(naive_mae, 0), round(naive_rmse, 0)
Part 12: Real Baseline 2, Moving Average#
A moving average smooths out single-week noise by averaging the most recent weeks. The code averages the last four training weeks with train.iloc[-4:].mean() and uses that one value as a flat forecast for all eight test weeks. The output shows an MAE of about 15,407 and an RMSE of about 21,369, far better than the naive forecast. Averaging four weeks captured the higher recent level better than a single week did.
ma_value = train.iloc[-4:].mean()
ma_forecast = pd.Series([ma_value] * len(test), index=test.index)
ma_mae, ma_rmse = mae(test, ma_forecast), rmse(test, ma_forecast)
round(ma_mae, 0), round(ma_rmse, 0)
Part 13: Real Model, Holt Exponential Smoothing with Trend#
Flat forecasts cannot follow a rising trend. Holt's exponential smoothing tracks both the level and the trend, giving more weight to recent weeks. ExponentialSmoothing(train, trend='add', seasonal=None) adds a straight-line trend with no seasonal pattern; strictly this is Holt's method, although the code labels it Holt-Winters. .fit() estimates the smoothing settings, and .forecast(len(test)) predicts eight weeks ahead. The output rises steadily from about 211,716 to 256,118, gaining roughly 6,300 a week.
from statsmodels.tsa.holtwinters import ExponentialSmoothing
hw_model = ExponentialSmoothing(train, trend='add', seasonal=None).fit()
hw_forecast = hw_model.forecast(len(test))
hw_forecast.round(0)
Part 14: Scoring the Real Holt-Winters Model#
Now you score the Holt model on the same test weeks using the same functions, so the comparison is fair. The output shows an MAE of about 16,324 and an RMSE of about 18,995. Its MAE is slightly worse than the moving average's 15,407, but its RMSE is better than the moving average's 21,369. That means Holt's forecast is a little less accurate on a typical week but avoids the very large misses, which RMSE punishes most.
hw_mae, hw_rmse = mae(test, hw_forecast), rmse(test, hw_forecast)
round(hw_mae, 0), round(hw_rmse, 0)
Part 15: Comparing All Three Real Models#
Putting every model in one table makes the choice clear. The code builds a DataFrame with one row per model and its MAE and RMSE. The naive forecast is clearly worst on both measures. The moving average has the lowest MAE (15,407), while Holt has the lowest RMSE (18,995). Which one wins depends on which error matters more to the business: if a single very bad week is costly, for example running out of stock before Christmas, RMSE is the better guide.
comparison = pd.DataFrame({
'Model': ['Naive', 'Moving Average', 'Holt-Winters'],
'MAE': [naive_mae, ma_mae, hw_mae],
'RMSE': [naive_rmse, ma_rmse, hw_rmse]
})
comparison.round(0)
Part 16: How Much Better Is the Real Winning Model#
Stakeholders understand 'percent better' more easily than raw error figures. The code takes the lowest MAE in the comparison table with .min() and calculates how much smaller it is than the naive MAE. The output says the best model cuts error by 67.3%. Be careful about which model that is: on MAE the best is the moving average, not Holt. The figure is accurate, but when you report it, name the model it belongs to.
best_model_mae = comparison['MAE'].min()
improvement_pct = round((naive_mae - best_model_mae) / naive_mae * 100, 1)
print(f'The best real model cuts forecast error by {improvement_pct}% versus the real naive baseline.')
Part 17: Visualizing Real Forecasts Against Real Actuals#
A chart of forecasts against actual values shows how and where each model goes wrong. The code plots the last 12 training weeks in grey, the actual test weeks in black, and the naive and Holt forecasts as dashed lines. The chart is saved to forecast_vs_actual.png. Look for the flat naive line sitting well below the actuals, and the Holt line rising steadily but not following the weekly ups and downs. The gap between the black and dashed lines is the error being measured.
plt.figure(figsize=(11, 6))
plt.plot(train.index[-12:], train.values[-12:], label='Real Training History', color='gray')
plt.plot(test.index, test.values, label='Real Actual', color='black', marker='o', linewidth=2)
plt.plot(test.index, naive_forecast.values, label='Real Naive Forecast', linestyle='--')
plt.plot(test.index, hw_forecast.values, label='Real Holt-Winters Forecast', linestyle='--')
plt.xlabel('Real Week')
plt.ylabel('Real Weekly Revenue (GBP)')
plt.title('Real Forecasts vs Real Actual Held-Out Weeks')
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig('forecast_vs_actual.png', dpi=120)
plt.close()
Part 18: Refitting on All Real Available Data#
Once a method has been tested honestly, you refit it on all available data so the final forecast uses the most recent weeks, including the eight used for testing. The code fits the same Holt model on the full weekly series and forecasts six weeks ahead. The output rises from about 270,317 for the week ending 11 December 2011 to 301,505 by mid-January 2012. Be cautious: the model has no seasonality, so it does not know that sales usually drop after Christmas, as the December 2010 data showed.
final_model = ExponentialSmoothing(weekly, trend='add', seasonal=None).fit()
future_forecast = final_model.forecast(6)
future_forecast.round(0)
Part 19: Visualizing the Real Future Forecast#
A chart that joins history and forecast lets you sense-check the forecast against what has happened before. The code draws all historical weeks in navy and the six forecast weeks in red with dots and a dashed line, then saves it to future_revenue_forecast.png. Look at whether the forecast continues the late-2011 climb, and compare it with what happened in the same weeks a year earlier. A forecast that keeps rising through Christmas and January would be unusual for a gift retailer.
plt.figure(figsize=(11, 6))
plt.plot(weekly.index, weekly.values, label='Real Historical Revenue', color='navy')
plt.plot(future_forecast.index, future_forecast.values, label='Real Future Forecast', color='crimson', marker='o', linestyle='--')
plt.xlabel('Real Week')
plt.ylabel('Real Weekly Revenue (GBP)')
plt.title('Real Historical Revenue Plus Six-Week Forecast')
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig('future_revenue_forecast.png', dpi=120)
plt.close()
Part 20: Real Residual Check#
Residuals are the differences between actual values and what the model fitted on the training data. A good model has residuals centred on zero with no pattern. hw_model.fittedvalues gives the in-sample predictions, and the code reports the mean and standard deviation of the residuals. The mean is about 9,411.5, so the model tends to underestimate revenue. The standard deviation is about 47,785.6, which is large compared with the test errors and reflects big swings in weeks such as Christmas.
residuals = train - hw_model.fittedvalues
residuals.mean().round(1), residuals.std().round(1)
Part 21: Saving the Real Forecast Results#
Saving the forecast makes it usable by a planning team or a spreadsheet. The code builds a two-column DataFrame with the week and the forecast value rounded to two decimals, writes it to weekly_revenue_forecast.csv with index=False, and reloads it. The output True confirms all six forecast weeks were saved. Tip: when sharing a forecast, also share the test error, so people know how much to trust each number.
forecast_output = pd.DataFrame({'Week': future_forecast.index, 'ForecastedRevenue': future_forecast.values.round(2)})
forecast_output.to_csv('weekly_revenue_forecast.csv', index=False)
reloaded_forecast = pd.read_csv('weekly_revenue_forecast.csv')
len(reloaded_forecast) == len(future_forecast)
Part 22: Real Percentage Growth Implied by the Forecast#
Expressing the forecast as growth makes it easy to judge whether it is believable. The code compares the first forecast week with the most recent actual week and prints the percentage change. The output says the model expects revenue to move 9.5% versus the latest week. With a trend-only model this simply continues the recent climb, so ask whether a further rise of that size is realistic in mid-December, when online gift orders usually start to tail off.
last_actual_week = weekly.iloc[-1]
first_forecast_week = future_forecast.iloc[0]
growth_pct = round((first_forecast_week - last_actual_week) / last_actual_week * 100, 1)
print(f"The model expects next week's real revenue to move {growth_pct}% versus the most recent actual real week.")
Part 23: Real Rolling Backtest#
One test period can be lucky or unlucky. A rolling backtest repeats the honest test at several cut-off points. For each cutoff of -20, -16, -12 and -8 weeks, the code trains on everything before it, forecasts the next four weeks and records the MAE. The errors are about 29,268, 24,092, 110,298 and 16,628. The third fold, which trains up to mid-September and forecasts the start of the autumn surge, is much worse. This shows the model struggles most when the trend changes suddenly.
backtest_errors = []
for cutoff in [-20, -16, -12, -8]:
fold_train, fold_test = weekly.iloc[:cutoff], weekly.iloc[cutoff:cutoff+4]
fold_model = ExponentialSmoothing(fold_train, trend='add', seasonal=None).fit()
fold_forecast = fold_model.forecast(len(fold_test))
backtest_errors.append(mae(fold_test, fold_forecast))
[round(e, 0) for e in backtest_errors]
Part 24: Real Backtest Stability#
Averaging backtest errors gives a more realistic expected error than a single test, and the spread shows how stable the model is. np.mean and np.std summarise the four fold errors. The average MAE is about 45,071 with a standard deviation of about 37,926. The spread is large because of the one very bad fold. In practice you would say the model is usually off by tens of thousands of pounds a week, and can miss by much more when demand changes quickly.
round(np.mean(backtest_errors), 0), round(np.std(backtest_errors), 0)
Part 25: Real Baseline 4, Simple Linear Trend#
A straight line fitted through all past weeks is another common baseline worth checking. np.polyfit(x, train.values, 1) fits a line of degree 1 to the training weeks numbered 0 to 43, returning a slope and intercept. The code then extends that line over the eight test weeks. The output shows an MAE of about 47,703 and an RMSE of about 51,075, almost as poor as the naive forecast. A line fitted to the whole year is too flat to capture the late surge, unlike Holt, which weights recent weeks more.
x = np.arange(len(train))
slope, intercept = np.polyfit(x, train.values, 1)
future_x = np.arange(len(train), len(train) + len(test))
linear_forecast = pd.Series(slope * future_x + intercept, index=test.index)
linear_mae, linear_rmse = mae(test, linear_forecast), rmse(test, linear_forecast)
round(linear_mae, 0), round(linear_rmse, 0)
Part 26: Real Total Forecasted Revenue#
Planners often need one total for the coming period, not just weekly values. future_forecast.sum() adds up the six forecast weeks, and the f-string format :,.0f prints it with comma separators and no decimals. The output is 1,715,468 GBP for the next six weeks. Keep in mind this assumes the rising trend continues through Christmas and into January, so the total is likely on the high side.
total_forecast_revenue = future_forecast.sum()
print(f'Total real forecasted revenue for the next six weeks: {round(total_forecast_revenue, 0):,.0f} GBP')
Part 27: Real Largest Single-Week Forecast Miss#
Knowing your worst miss helps you explain the forecast's limits. The code takes the absolute difference between actual and Holt forecast for each test week, finds the week with the largest error using idxmax(), and prints it. The worst week was the one ending 23 October 2011, when actual revenue was 245,694 against a forecast of 218,059, an under-forecast of about 27,600. Look at which direction your biggest misses go, because under-forecasting can mean running out of stock.
errors_by_week = (test - hw_forecast).abs()
worst_week = errors_by_week.idxmax()
print(f'Largest real miss: week of {worst_week.date()}, real actual {test[worst_week]:,.0f} vs real forecast {hw_forecast[worst_week]:,.0f}')
Part 28: Real Final Model Comparison Including Linear Trend#
A final table that includes every model makes the decision easy to document. pd.concat adds the linear trend row to the earlier comparison table, with ignore_index=True to renumber the rows. The sorting line is not the last line, so nothing is displayed; the table is saved to forecast_model_comparison.csv. Run the sort_values('MAE') line on its own: you should see the moving average first, then Holt, then the naive forecast and the linear trend close together at the bottom.
full_comparison = pd.concat([comparison, pd.DataFrame({'Model': ['Linear Trend'], 'MAE': [linear_mae], 'RMSE': [linear_rmse]})], ignore_index=True)
full_comparison.sort_values('MAE').round(0)
full_comparison.to_csv('forecast_model_comparison.csv', index=False)
Part 29: Real Weekly Average Sanity Check#
Resampling and trimming are easy to get subtly wrong, so you verify one weekly total by hand. The code takes the first week's end date, goes back six days with pd.Timedelta(days=6), slices those seven days from the daily series with .loc and sums them. It then checks the difference from the weekly value is under 0.01, which allows for tiny rounding differences in decimal numbers. The output is True, so the weekly series matches the underlying daily data.
manual_weekly_total = daily.loc[weekly.index[0] - pd.Timedelta(days=6):weekly.index[0]].sum()
abs(manual_weekly_total - weekly.iloc[0]) < 0.01
Part 30: Real Recap Print#
A one-line recap states what was done and how well it worked. The code prints the number of future weeks and the improvement over the naive baseline. The output reads: 6 future weeks forecast using Holt-Winters, beating the naive baseline by 67.3%. As noted earlier, that 67.3% is the moving average's MAE improvement; Holt's own MAE improvement is smaller, about 65% based on the table. When you write a recap, make sure each figure is tied to the right model.
print(f'Forecasted {len(future_forecast)} real future weeks using Holt-Winters, beating the naive baseline by {improvement_pct}% on the real held-out test.')
Wrap-Up: What You Learned#
- Forecasting starts with one real evenly-spaced timeline; missing real days are genuinely zero, not gaps to drop.
- A real honest test hides real recent weeks and scores forecasts against them, never against data the model already saw.
- Naive and moving-average forecasts are real fast, genuinely useful floors that any smarter model has to actually beat.
- Holt's exponential smoothing tracks a real trend, letting the forecast keep moving instead of staying flat.
- A real production forecast gets refit on every real available week once the method has proven itself honestly.
- Next video: real price and discount impact analysis, looking at how real markdowns actually move real demand.
Practice on your own
- Change the moving average window from 4 weeks to 2, 6 and 8 weeks, and record the MAE and RMSE for each on the same eight test weeks.
- Recalculate the improvement over the naive baseline separately for the moving average and for Holt, and write your own one-line recap that ties each figure to its model.
- Add a fifth fold to the rolling backtest with a cutoff of -24 weeks, and see how the average and spread of the backtest errors change.
The next video in the series covers price and discount impact analysis, looking at how markdowns actually move demand.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



