Khansa, home
Work with me

Jump to

Pages

Projects

Links

Lesson 7 of 8, about 18 minutes

Forecasting with machine learning and Prophet in Python

You can forecast with any machine learning model if you give it the right columns. The trick is to build features from the past: yesterday’s value, last week’s value, the weekday. Prophet does much of this for you.

By the end: Forecast with a machine learning model and with Prophet.

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.

Python
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.

Python
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.

Python
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()

Where you will use this

  • Forecasts that depend on extra information such as price, weather or promotions
  • Many products at once, with one shared model
  • A quick first forecast with holidays included, using Prophet

Common mistake: Data leakage: if a feature uses information from the day you are predicting or later, your test score will look great and your real forecasts will fail.

Try it yourself

Remove the shift(1) from the roll7 line and rerun. Watch the score jump. That jump is leakage, not skill.

Check yourself

Why shift(1) before rolling?

So the average only uses days before the one you predict, and never includes it.

What two columns does Prophet need?

ds for the date and y for the value.