First set up the requirements:
In [1]:
from fbprophet import Prophet
import pandas as pd
%matplotlib notebook
import matplotlib
Sample road traffic data in CSV format can be downloaded here (for the sake of this example, dataset is included in the repository).
Load the data parsing dates as required. We also rename columns to ds and y in accordance with the convention used by Prophet.
In [2]:
date_parse = lambda date: pd.datetime.strptime(date, '%Y-%m-%d')
time_series = pd.read_csv("solarhringsumferd-a-talningarsto.csv", header=0, names=['ds', 'y'], usecols=[0, 1],
parse_dates=[0], date_parser=date_parse)
Split data into two sets - one for training and one for testing our model:
In [3]:
training = time_series[time_series['ds'] < '2009-01-01']
testing = time_series[time_series['ds'] > '2009-01-01']
testing.columns = ['ds', 'ytest']
training.plot(x='ds');
All that is required to do the forecast:
In [4]:
# Train the model.
model = Prophet()
model.fit(training)
# Define period to make forecast for.
future = model.make_future_dataframe(periods=365*2)
# Perform prediction for the defined period.
predicted_full = model.predict(future)
# We only plot date and predicted value.
# Full prediction contains much more data, like confidence intervals, for example.
predicted = predicted_full[['ds', 'yhat']]
# Plot training, testing and predicted values together.
combined = training.merge(testing, on='ds', how='outer')
combined = combined.merge(predicted, on='ds', how='outer')
combined.columns = ['Date', 'Training', 'Testing', 'Predicted']
# Only show the "intersting part" - no point in looking at the past.
combined[combined['Date'] > '2008-01-01'].plot(x='Date');
We can also query our model to show us trend, weekly and yearly components of the forecast:
In [5]:
model.plot_components(predicted_full);
Thank you!