Prophet › Example
Michael BrenndoerferAugust 5, 20253 min read
Machine Learning from ScratchAll chapters Part 8 of 9Time Series
Prophet › ExampleOne section · Sections in this chapter4 / 8 ↓
Sections in this chapter8
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's work through a concrete example using Prophet to forecast monthly sales data. We'll use simple numbers to make the calculations easy to follow and demonstrate how Prophet decomposes the time series.
In[7]:
Codeimport numpy as np
import pandas as pd
# Create sample monthly sales data (2020-2023)
np.random.seed(42)
dates = pd.date_range(start="2020-01-01", end="2023-12-31", freq="ME")
# Create realistic sales data with trend and seasonality
base_sales = 1000
trend_growth = 50 # Monthly growth
seasonal_amplitude = 200
noise_std = 50
sales_data = []
for i, date in enumerate(dates):
# Trend component: linear growth
trend = base_sales + trend_growth * i
# Seasonal component: higher in December, lower in summer
month = date.month
if month == 12: # December peak
seasonal = seasonal_amplitude
elif month in [6, 7, 8]: # Summer dip
seasonal = -seasonal_amplitude * 0.5
else:
seasonal = seasonal_amplitude * 0.3 * np.sin(2 * np.pi * month / 12)
# Add noise
noise = np.random.normal(0, noise_std)
# Total sales
total_sales = trend + seasonal + noise
sales_data.append(total_sales)
# Create DataFrame
df = pd.DataFrame({"ds": dates, "y": sales_data})
print("Sample of our sales data:")
print(df.head(10))
print(f"\nData shape: {df.shape}")
print(f"Date range: {df['ds'].min()} to {df['ds'].max()}")Out[8]:
ConsoleSample of our sales data:
ds y
0 2020-01-31 1054.835708
1 2020-02-29 1095.048309
2 2020-03-31 1192.384427
3 2020-04-30 1278.113017
4 2020-05-31 1218.292331
5 2020-06-30 1138.293152
6 2020-07-31 1278.960641
7 2020-08-31 1288.371736
8 2020-09-30 1316.526281
9 2020-10-31 1425.166478
Data shape: (48, 2)
Date range: 2020-01-31 00:00:00 to 2023-12-31 00:00:00Now let's fit a Prophet model to this data and examine the components:
In[9]:
Code# Initialize and fit Prophet model
model = Prophet(
yearly_seasonality=True,
weekly_seasonality=False, # We have monthly data
daily_seasonality=False,
changepoint_prior_scale=0.05, # Controls trend flexibility
seasonality_prior_scale=10.0, # Controls seasonality strength
)
# Fit the model
model.fit(df)
# Make predictions
future = model.make_future_dataframe(
periods=12, freq="ME"
) # Forecast 12 months ahead
forecast = model.predict(future)
print("Forecast components for the last few months:")
print(forecast[["ds", "yhat", "trend", "yearly"]].tail(15))Out[10]:
ConsoleForecast components for the last few months:
ds yhat trend yearly
45 2023-10-31 3151.874692 3233.631097 -81.756406
46 2023-11-30 3280.539958 3282.505497 -1.965539
47 2023-12-31 3562.361704 3333.009044 229.352660
48 2024-01-31 3406.825196 3383.512591 23.312605
49 2024-02-29 3444.989486 3430.757844 14.231642
50 2024-03-31 3527.736876 3481.261391 46.475485
51 2024-04-30 3620.414953 3530.135791 90.279162
52 2024-05-31 3568.556723 3580.639338 -12.082614
53 2024-06-30 3522.374830 3629.513738 -107.138908
54 2024-07-31 3612.570060 3680.017284 -67.447224
55 2024-08-31 3637.671445 3730.520831 -92.849386
56 2024-09-30 3745.608481 3779.395231 -33.786750
57 2024-10-31 3794.709579 3829.898778 -35.189199
58 2024-11-30 3847.131643 3878.773178 -31.641535
59 2024-12-31 4075.134016 3929.276724 145.857291Let's visualize the results to understand how Prophet decomposed our sales data:
Out[11]:
Visualization