While advancements in data science often increase the infamous “skills gap” surrounding the field, Prophet was intentionally designed to lower the cost of entry for “analysts” — who possess an “in-the-loop” understanding of the problems they are trying to solve — via automation of time series forecasting.

Prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. It works best with time series that have strong seasonal effects and several seasons of historical data. Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.

Prophet is open source software released by Facebook’s Core Data Science team. It is available for download on CRAN and PyPI.

The procedure makes use of a decomposable time series model with three main model components: trendseasonality, and holidays.

y(t) = g(t) + s(t) + h(t) + e(t)

g(t)

  • trend models non-periodic changes; linear or logistic

s(t)

  • seasonality represents periodic changes; i.e. weekly, monthly, yearly

h(t)

  • ties in effects of holidays; on potentially irregular schedules ≥ 1 day(s)

Installation

  • pip install pystan
  • pip install fbprophet
  • conda install -c conda-forge fbprophet

Intro To Facebook Prophet

  • Steps
    • Initialize Model :: Prophet()
    • Set columns as ds,y
    • Fit dataset :: Prophet().fit()
    • Create Dates To predict :: Prophet().make_future_dataframe(periods=365)
    • Predict :: Prophet().predict(future_dates)
    • Plot :: Prophet().plot(predictions)

In [ ]:

# Load EDA Pkgs
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

In [ ]:

# Load FB Prophet
import fbprophet

In [ ]:

dir(fbprophet)

Out[ ]:

['Prophet',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '__version__',
 'diagnostics',
 'forecaster',
 'hdays',
 'make_holidays',
 'models',
 'plot']

In [ ]:

# Load our Dataset
df = pd.read_csv("flights_data.csv")

In [ ]:

df.head()

Out[ ]:

Datesno_of_flights
02005-01-01594924
12005-02-01545332
22005-03-01617540
32005-04-01594492
42005-05-01614802

In [ ]:

df.plot()

Out[ ]:

<matplotlib.axes._subplots.AxesSubplot at 0x21ac5193988>

In [ ]:

#yt = yt -y(t-1)
df['no_of_flights'] = df['no_of_flights'] - df['no_of_flights'].shift(1)

In [ ]:

df.plot()

Out[ ]:

<matplotlib.axes._subplots.AxesSubplot at 0x21ac5300188>

In [ ]:

from fbprophet import Prophet

In [ ]:

# Features of Prophet
dir(Prophet)

Out [ ]:

['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_load_stan_backend',
 'add_country_holidays',
 'add_group_component',
 'add_regressor',
 'add_seasonality',
 'construct_holiday_dataframe',
 'fit',
 'fourier_series',
 'initialize_scales',
 'linear_growth_init',
 'logistic_growth_init',
 'make_all_seasonality_features',
 'make_future_dataframe',
 'make_holiday_features',
 'make_seasonality_features',
 'parse_seasonality_args',
 'percentile',
 'piecewise_linear',
 'piecewise_logistic',
 'plot',
 'plot_components',
 'predict',
 'predict_seasonal_components',
 'predict_trend',
 'predict_uncertainty',
 'predictive_samples',
 'regressor_column_matrix',
 'sample_model',
 'sample_posterior_predictive',
 'sample_predictive_trend',
 'set_auto_seasonalities',
 'set_changepoints',
 'setup_dataframe',
 'validate_column_name',
 'validate_inputs']

In [ ]:

# Initialize the Model
model = Prophet()

Parameters

  • growth: linear/logistic
  • seasonality:additive/multiplicative
  • holidays:
  • changepoint:

df.columns

Out[ ]:

Index(['Dates', 'no_of_flights'], dtype='object')

In [ ]:

# Works with a ds and y column names
df.rename(columns={'Dates':'ds','no_of_flights':'y'},inplace=True)

In [ ]:

df.head()

Out[ ]:

dsy
02005-01-01NaN
12005-02-01-49592.0
22005-03-0172208.0
32005-04-01-23048.0
42005-05-0120310.0

In [ ]:

df = df[1:]

In [ ]:

df.head()

Out[ ]:

dsy
12005-02-01-49592.0
22005-03-0172208.0
32005-04-01-23048.0
42005-05-0120310.0
52005-06-01-5607.0

In [ ]:

# Fit our Model to our Data
model.fit(df)

Out[ ]:

<fbprophet.forecaster.Prophet at 0x21ac544de08>

In [ ]:

# Shape of Dataset
df.shape

Out[ ]:

(35, 2)

In [ ]:

# Create Future Dates of 365 days
future_dates = model.make_future_dataframe(periods=365)

In [ ]:

# Shape after adding 365 days
future_dates.shape

Out[ ]:

(400, 1)

In [ ]:

future_dates.head()

Out[56]:

ds
02005-02-01
12005-03-01
22005-04-01
32005-05-01
42005-06-01

In [ ]:

# Make Prediction with our Model
prediction = model.predict(future_dates)

In [ ]:

prediction.head()

Out[ ]:

dstrendyhat_loweryhat_uppertrend_lowertrend_upperadditive_termsadditive_terms_loweradditive_terms_upperyearlyyearly_loweryearly_uppermultiplicative_termsmultiplicative_terms_lowermultiplicative_terms_upperyhat
02005-02-01-2571.714150-52813.083521-46164.594330-2571.714150-2571.714150-46882.135892-46882.135892-46882.135892-46882.135892-46882.135892-46882.1358920.00.00.0-49453.850042
12005-03-01-2445.18998567319.74320674045.134070-2445.189985-2445.18998573145.01289473145.01289473145.01289473145.01289473145.01289473145.0128940.00.00.070699.822909
22005-04-01-2305.109659-27300.668683-20707.417240-2305.109659-2305.109659-21686.914066-21686.914066-21686.914066-21686.914066-21686.914066-21686.9140660.00.00.0-23992.023726
32005-05-01-2169.54805414332.14971821283.340405-2169.548054-2169.54805419993.46427119993.46427119993.46427119993.46427119993.46427119993.4642710.00.00.017823.916217
42005-06-01-2029.467726-10647.000844-3755.469838-2029.467726-2029.467726-5302.735919-5302.735919-5302.735919-5302.735919-5302.735919-5302.7359190.00.00.0-7332.203646

Narrative

  • yhat : the predicted forecast
  • yhat_lower : the lower border of the prediction
  • yhat_upper: the upper border of the prediction

In [ ]:

# Plot Our Predictions
model.plot(prediction)

Out[ ]:

Narrative

  • A Trending data
  • Black dots : the actual data points in our dataset.
  • Deep blue line : the predicted forecast/the predicted values
  • Light blue line : the boundaries

In [ ]:

# Visualize Each Component [Trends,Weekly]
model.plot_components(prediction)

Out[ ]:

Cross Validation

  • For measuring forecast error by comparing the predicted values with the actual values
  • initial:the size of the initial training period
  • period : the spacing between cutoff dates
  • horizon : the forecast horizon((ds minus cutoff)
  • By default, the initial training period is set to three times the horizon, and cutoffs are made every half a horizon

In [ ]:

# Load Pkgs
from fbprophet.diagnostics import cross_validation

In [ ]:

df.shape

Out[ ]:

(35, 2)

In [ ]:

cv = cross_validation(model,initial='35 days', period='180 days', horizon = '365 days')

In [ ]:

cv.head()

Out[ ]:

dsyhatyhat_loweryhat_upperycutoff
02005-07-01-2.998956e+05-2.998956e+05-2.998956e+0518766.02005-06-09
12005-08-01-1.506471e+06-1.506471e+06-1.506471e+062943.02005-06-09
22005-09-014.293684e+034.293683e+034.293685e+03-56651.02005-06-09
32005-10-011.213440e+061.213440e+061.213440e+0618459.02005-06-09
42005-11-01-2.180407e+05-2.180407e+05-2.180407e+05-26574.02005-06-09

Performance Metrics

In [ ]:

from fbprophet.diagnostics import performance_metrics

In [ ]:

df_pm = performance_metrics(cv)

In [ ]:

df_pm

Out[ ]:

horizonmsermsemaemapemdapecoverage
031 days2.692910e+10164100.895645102813.8135746.5898544.5970270.00
153 days5.711252e+11755728.239683400501.823432130.5653164.5970270.00
257 days5.827072e+11763352.591903438304.026472129.5396742.5457420.00
358 days5.826882e+11763340.159421437355.801093129.6807062.8278060.00
462 days5.827802e+11763400.445519441439.714453129.7214042.8278060.00
584 days1.412643e+10118854.67944779322.4805411.7696911.0795640.00
685 days1.410763e+10118775.52568179281.3608601.3993421.0795640.00
789 days1.409591e+10118726.19485778341.3665371.1533510.7115760.25
890 days1.406118e+10118579.83878477345.9627161.1190030.6428820.25
9114 days3.701274e+11608380.996828360855.01033417.0343131.6504240.25
10116 days3.671415e+11605922.017604353902.09999818.7887835.1593630.25
11119 days3.671453e+11605925.155903354275.09960818.8184945.1634850.25
12121 days3.671874e+11605959.904282355456.77769118.9714255.4652250.25
13145 days1.935746e+10139131.074345104578.2306584.5884264.0133670.25
14146 days2.208847e+10148621.914355110970.8876855.2828784.0133670.25
15150 days2.220278e+10149005.973914115051.6507415.4454704.0368100.00
16151 days2.211875e+10148723.733688112115.3621585.3643984.0368100.00
17175 days6.795244e+10260676.880961181507.54978822.4606186.8775730.00
18177 days6.598742e+10256880.170222176987.16550130.55031523.0569680.00
19180 days6.586463e+10256641.044587171556.03129030.33913022.8713800.25
20182 days6.586411e+10256640.037341171525.16483731.10712024.4073600.25
21206 days8.747295e+10295758.254878192656.51192927.68260724.4073600.25
22207 days9.046698e+10300777.288635199405.66704018.9368656.9158760.25
23211 days9.049226e+10300819.307680201811.34758119.0806766.9158760.25
24212 days9.054020e+10300898.979715203770.93070818.3977665.5500560.25
25237 days3.058308e+10174880.177196129898.7010324.2141282.9975200.25
26238 days2.773357e+10166533.989174123495.2981678.6494872.9975200.25
27242 days2.772052e+10166494.800664122730.8748088.5310242.9975200.25
28243 days2.770857e+10166458.918733122342.3364139.0949154.1253010.25
29265 days2.421694e+10155617.925574115179.9270208.5809763.0974240.25
30269 days2.778923e+10166701.023204123099.6282982.9578693.0974240.25
31270 days2.777735e+10166665.386503121662.5713022.9308383.0974240.25
32274 days2.792908e+10167119.947533125172.0964102.2923531.8204560.25
33296 days3.008901e+10173461.837321129700.6477194.6320413.0253730.25
34299 days2.284019e+10151129.725412112125.2611204.8511263.4635430.25
35301 days2.284588e+10151148.551953113044.0488094.8970763.4635430.25
36304 days2.266595e+10150552.134430108388.0159324.8462103.3618110.25
37326 days2.142954e+10146388.325580105838.0637645.1085333.3618110.25
38330 days3.116496e+10176536.007400128361.5701616.0699235.2845920.25
39331 days3.116747e+10176543.098562128593.4705066.1061025.2894590.25
40335 days3.114527e+10176480.218194126935.9241156.0561535.2894590.50
41357 days3.485784e+10186702.551971134300.23728017.1658255.2894590.50
42360 days2.386630e+10154487.217191107932.59353816.7045514.3669100.50
43362 days2.386316e+10154477.050064107634.30704417.1193225.1964530.50
44365 days2.400036e+10154920.493560112706.36032717.7435095.5105190.25

Visualizing Performance Metrics

  • cutoff: how far into the future the prediction was

In [ ]:

from fbprophet.plot import plot_cross_validation_metric

In [ ]:

plot_cross_validation_metric(cv,metric='rmse')

Out[ ]: