Prophet 101: a time-series forecasting module

Goal

This post aims to introduce the basics of Prophet, which is a time-series forecasting module implemented by Facebook.

image

Reference

Libraries

In [1]:
import pandas as pd
from fbprophet import Prophet
from fbprophet.plot import plot_plotly
import plotly.offline as py
py.init_notebook_mode()

Load a time series data

In [11]:
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv").iloc[:, :2]
df.head()
Out[11]:
Date AAPL.Open
0 2015-02-17 127.489998
1 2015-02-18 127.629997
2 2015-02-19 128.479996
3 2015-02-20 128.619995
4 2015-02-23 130.020004

modify the data for Prophet

In [12]:
df.rename(columns={'Date': 'ds', 'AAPL.Open': 'y'}, inplace=True)
df.head()
Out[12]:
ds y
0 2015-02-17 127.489998
1 2015-02-18 127.629997
2 2015-02-19 128.479996
3 2015-02-20 128.619995
4 2015-02-23 130.020004
In [15]:
df.tail()
Out[15]:
ds y
501 2017-02-10 132.460007
502 2017-02-13 133.080002
503 2017-02-14 133.470001
504 2017-02-15 135.520004
505 2017-02-16 135.669998

Create a Prophet instance

In [13]:
m = Prophet()
m.fit(df)
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
/Users/hiro/anaconda3/envs/py367/lib/python3.6/site-packages/pystan/misc.py:399: FutureWarning:

Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.

Out[13]:
<fbprophet.forecaster.Prophet at 0x122dd8588>

Make a prediction

Create a future date range

In [17]:
date_future = m.make_future_dataframe(periods=365)
date_future.head()
Out[17]:
ds
0 2015-02-17
1 2015-02-18
2 2015-02-19
3 2015-02-20
4 2015-02-23
In [18]:
date_future.tail()
Out[18]:
ds
866 2018-02-12
867 2018-02-13
868 2018-02-14
869 2018-02-15
870 2018-02-16

Make a prediction

In [19]:
df_forecast = m.predict(date_future)
df_forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
Out[19]:
ds yhat yhat_lower yhat_upper
866 2018-02-12 209.525238 155.915802 260.598412
867 2018-02-13 210.456875 157.044369 262.299782
868 2018-02-14 210.871056 156.648230 263.840901
869 2018-02-15 211.553346 156.689575 263.786439
870 2018-02-16 211.947448 156.875387 263.353519

Visualization

Visualize the values with confidence

In [20]:
fig1 = m.plot(df_forecast)

Visualize by components

In [22]:
fig2 = m.plot_components(df_forecast)

Visualize interactive plots

In [ ]:
fig = plot_plotly(m, df_forecast)  
py.iplot(fig)

Comments

Comments powered by Disqus