Lok at time series of daily page views fro the Wikipedia page for Peyton Manning. The csv is available here
In [9]:
peyton_dataset_url = 'https://github.com/facebookincubator/prophet/blob/master/examples/example_wp_peyton_manning.csv'
peyton_filename = '../datasets/example_wp_peyton_manning.csv'
In [3]:
import pandas as pd
import numpy as np
from fbprophet import Prophet
In [10]:
# NB: this didn't work as of 8/22/17
#import io
#import requests
#s=requests.get(peyton_dataset_url).content
#df=pd.read_csv(io.StringIO(s.decode('utf-8')))#df = pd.read_csv(peyton_dataset_url)
In [11]:
df = pd.read_csv(peyton_filename)
In [12]:
# transform to log scale
df['y']=np.log(df['y'])
df.head()
Out[12]:
Fit the model by instantiating a new Prophet
object. Any settings required for the forecasting procedure are passed to this object upon construction. You then can call this object's fit
method and pass in the historical dataframe. Fitting should take 1-5 seconds.
In [13]:
m = Prophet()
m.fit(df);
Predictions are then made on a dataframe with a column ds
containing the dates for which a prediction is to be made. You can get a suitable dataframe that extends into the future a specified number of days using the helper method Prophet.make_future_dataframe
. By default it will also include the dates from the history, so we will see the model fit as well.
In [14]:
future = m.make_future_dataframe(periods=365)
future.tail()
Out[14]:
The predict
method will assign each row in future
a predicted value which it names yhat
. If you pass in historical dates, it will provide an in-sample fit. The forecast
object here is a new dataframe that includes a column yhat
with the forecast, as well as columns for components and uncertainty intervals.
In [15]:
forecast = m.predict(future)
forecast[['ds','yhat','yhat_lower','yhat_upper']].tail()
Out[15]:
You can plot the forecast by calling the Prophet.plot
method and passing in your forecast dataframe
In [16]:
m.plot(forecast)
Out[16]:
If you want to see the forecast components, you can use the Prophet.plot_components
method. By default you’ll see the trend, yearly seasonality, and weekly seasonality of the time series.
If you include holidays, you’ll see those here, too.
In [17]:
m.plot_components(forecast)
Out[17]:
In [ ]: