Module 14 · Capstone Projects: Three Portfolio Pieces · Lesson 46 of 51
Capstone 1: Forecast sales to plan staffing
The owner wants next week's sales per branch to plan shifts. Explore, build lag and calendar features, split by time, beat a seasonal baseline with a model, and write the recommendation.
In this lesson · about 75 minutes
- Frame a forecasting problem and choose a time-based split
- Build lag, rolling and calendar features without leaking the future
- Compare models against a seasonal-naive baseline and explain the result
Your plan
- Explore: sales by weekday, by branch, against temperature and promos. Plot a branch over time.
- Baseline: "same weekday last week" (seasonal naive). Any model must beat it.
- Features: weekday, weekend, promo (planned in advance, so known), temperature (from the forecast),
sales_lag7(same day last week) andsales_avg7(average of the previous 7 days). - Split by time: train January–May, test June.
- Models: linear regression and gradient boosting, in pipelines; compare MAE on June.
- Recommend: in plain words, with the error in dollars and what it means for staffing.
Step 1: explore
Exercise
Step 2: the seasonal-naive baseline
The features and the June test set are ready (sales_lag7 is the same branch's sales 7 days earlier). Set naive_mae to the mean absolute error on test of predicting sales_lag7, and mean_mae to the MAE of predicting each branch's training mean. Round both to 1 decimal.
Code that runs before yours
import pandas as pd
import numpy as np
daily = pd.read_csv("kc_daily.csv", parse_dates=["date"]).sort_values(["branch", "date"]).reset_index(drop=True)
daily["dow"] = daily["date"].dt.dayofweek
daily["sales_lag7"] = daily.groupby("branch")["sales"].shift(7)
daily["sales_avg7"] = daily.groupby("branch")["sales"].transform(lambda s: s.shift(1).rolling(7).mean())
data = daily.dropna().copy()
train = data[data["date"] < "2026-06-01"]
test = data[data["date"] >= "2026-06-01"]Exercise
Step 3: a model that beats it
Fit two pipelines on train with features temp_c, rain_mm, promo, weekend, sales_lag7, sales_avg7 (numbers) and branch, dow (one-hot): a LinearRegression and a HistGradientBoostingRegressor(max_depth=3, learning_rate=0.05, max_iter=300, random_state=0). Set results to a dict {"linear": mae, "boosting": mae} of June MAEs rounded to 1 decimal, and winner to the name with the lower MAE.
Code that runs before yours
import pandas as pd
import numpy as np
daily = pd.read_csv("kc_daily.csv", parse_dates=["date"]).sort_values(["branch", "date"]).reset_index(drop=True)
daily["dow"] = daily["date"].dt.dayofweek
daily["sales_lag7"] = daily.groupby("branch")["sales"].shift(7)
daily["sales_avg7"] = daily.groupby("branch")["sales"].transform(lambda s: s.shift(1).rolling(7).mean())
data = daily.dropna().copy()
train = data[data["date"] < "2026-06-01"]
test = data[data["date"] >= "2026-06-01"]
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.metrics import mean_absolute_error
num = ["temp_c", "rain_mm", "promo", "weekend", "sales_lag7", "sales_avg7"]
cat = ["branch", "dow"]Write the one-paragraph recommendation for the owner.
Consider these questions and write down your thoughts. What you write stays on this device.
Exercises solved: 0 of 2
Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.