Prophet – Changing Seasonality
Changing Seasonality with Prophet
Load Data
Load Airline dataset
import pandas as pd
import numpy as np
from fbprophet import Prophet
df = pd.read_csv('airline_passengers.csv')
Changes in seasonality can be either additive or multiplicative so we have to keep that in mind when looking at seasonality.
Format Data to Prophet’s Specs
# rename columns
df.columns = ['ds', 'y']
# convert date index to timeseries
df['ds'] = pd.to_datetime(df['ds'])
df.head()
ds | y | |
---|---|---|
0 | 1949-01-01 | 112 |
1 | 1949-02-01 | 118 |
2 | 1949-03-01 | 132 |
3 | 1949-04-01 | 129 |
4 | 1949-05-01 | 121 |
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 144 entries, 0 to 143
Data columns (total 2 columns):
ds 144 non-null datetime64[ns]
y 144 non-null int64
dtypes: datetime64[ns](1), int64(1)
memory usage: 2.3 KB
df.describe()
y | |
---|---|
count | 144.000000 |
mean | 280.298611 |
std | 119.966317 |
min | 104.000000 |
25% | 180.000000 |
50% | 265.500000 |
75% | 360.500000 |
max | 622.000000 |
Create Prophet Model (Additive)
m = Prophet()
# fit all the data to the model
m.fit(df)
# create future dataframe
future = m.make_future_dataframe(50, freq='MS')
# create forecast
forecast = m.predict(future)
# plot
fig = m.plot(forecast)
Plot Forecast Components
fig = m.plot_components(forecast)
Add Changes to Plot
from fbprophet.plot import add_changepoints_to_plot
fig = m.plot(forecast)
a = add_changepoints_to_plot(fig.gca(), m, forecast)
Create Prophet Model (Multiplicative)
# define multiplicative model
m = Prophet(seasonality_mode='multiplicative')
# fit all the data to the model
m.fit(df)
# create future dataframe
future = m.make_future_dataframe(50, freq='MS')
# create forecast
forecast = m.predict(future)
# plot
fig = m.plot(forecast)
Plot indiviadual components
fig = m.plot_components(forecast)
Add Changes to Plot
from fbprophet.plot import add_changepoints_to_plot
fig = m.plot(forecast)
a = add_changepoints_to_plot(fig.gca(), m, forecast)