Build features from the past
A lag is the value from some days ago. A rolling mean is the average of recent days. We shift by one day before rolling, so the average never includes today. This is the most common place for hidden leaks.
import numpy as np
import pandas as pd
rng = np.random.default_rng(42)
days = pd.date_range("2024-01-01", periods=365, freq="D")
trend = np.linspace(100, 140, 365) # sales slowly grow
week = np.array([-8, -6, -4, 0, 6, 16, 12])[days.dayofweek] # busy weekends
s = pd.Series(trend + week + rng.normal(0, 4, 365), index=days, name="sales")
df = s.to_frame("y")
df["lag1"] = df["y"].shift(1)
df["lag7"] = df["y"].shift(7)
df["roll7"] = df["y"].shift(1).rolling(7).mean() # shift first, then roll
df["dow"] = df.index.dayofweek
df["t"] = np.arange(len(df))
df = df.dropna()
df.head(3)Train a model and test it by time
Gradient boosting works well on tables like this. Keep the last 28 days as the test, as always.
from sklearn.ensemble import HistGradientBoostingRegressor
X, y = df.drop(columns="y"), df["y"]
X_tr, X_te = X[:-28], X[-28:]
y_tr, y_te = y[:-28], y[-28:]
gb = HistGradientBoostingRegressor(random_state=0).fit(X_tr, y_tr)
pred = gb.predict(X_te)
print("MAE:", round(float(np.mean(np.abs(y_te.values - pred))), 2))You should see
MAE: 4.34
The noise we added to the data has a size of about 4, so an error near 4 is close to the best any model could do one day ahead.
Know the limit of this simple version
This test uses real lag values from the test days, so it forecasts one day ahead. To forecast 28 days ahead you have two options. Direct: train one model for each horizon. Recursive: feed each prediction back in as the next lag. Both are fine, but test the way you will really use it.
Prophet, the easy option
Prophet needs two columns, ds (date) and y (value). It finds trend, weekly and yearly patterns on its own, and can add holidays. Install it with pip install prophet.
from prophet import Prophet
p_df = s.reset_index()
p_df.columns = ["ds", "y"]
m = Prophet(weekly_seasonality=True, yearly_seasonality=False)
m.fit(p_df)
future = m.make_future_dataframe(periods=28)
forecast = m.predict(future)
forecast[["ds", "yhat", "yhat_lower", "yhat_upper"]].tail()