Prophet-able Forecasting
By: Werlindo Mangrobang
Tags: Time Series Forecasting, Towards Data Science, Data Science, Python, Prophet

3. Fitting the model and predicting

Next, we’ll run through an example of running a forecast:

Fitting the model

Predicting on our model

First, let’s instantiate our model and fit it on our dataset:

from fbprophet import Prophet #Importing here for visibility # Create Prophet object and fit

m_bikes = Prophet(yearly_seasonality=True

, weekly_seasonality=True) #All defaults

m_bikes.fit(trips)

The object Prophet has many parameters that can be set to tune the model. For example, seasonality settings such as daily_seasonality , weekly_seasonality , and yearly_seasonality are just some of the options that can be tuned. For example, I think there might be some weekly and/or yearly patterns for bike-share rentals. For fun, let’s see what happens if we set weekly_seasonality and yearly_seasonality to True .

Next, we can then perform predictions using the model. the Prophet object has a handy method called make_future_dataframe that can create a dataframe with a specified number of future dates, relative to the training data (days is the default periods ). For example, below we tack on 60 days after our training data’s last day.

# Create dataframe with appended future dates

future = m_bikes.make_future_dataframe(periods=60) #Days is default

future.tail()

Once, that dataframe is set up, we can then run predictions on that ‘test’ set:

# Forecast on future dates

forecast = m_bikes.predict(future) # Look at prediction and prediction interval

forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()

4. Inspecting the results

We already got a sneak preview into the predictions by looking at the tail of the forecast dataframe we created. Let’s visualize it though to get the big picture.

It just so happens that our Prophet object also includes a plot method to do just that! Here’s the plot of the fit/forecast (line (y_hat) and shaded area (prediction interval)) versus the actuals (scatter):

# Plot the forecast vs actuals

m_bikes.plot(forecast)

Not bad. Could potentially use some more data cleaning (outliers?) and parameter tuning.

In addition, there are plots for the forecast components as well! For example, since we explicitly set weekly_seasonality and yearly_seasonality to True , there will be component plots specifically for them:

Wooh! There’s nothin’ like summer in the city.

We could definitely use more historical data, but if we take these plots at face value, it appears there is a late summer spike in trips, and then it gradually dips over the fall. Furthermore, during the week, peak riding seemed to happen mid to late weekdays. That might not completely pass the sniff-test of my anecdotal observations if these were Seattle bike-shares, but hey, L.A. is probably a completely different market. The point is though we were able to create some decent forecasts in a short amount of time!

So much more…

If we had more time to dive in more we could start looking at the aforementioned parameters to further tune the forecasts. These include not only seasonality for different periods, but specific holiday effects, providing specific changepoints for trend (e.g.you don’t have a perfectly linear baseline trend, such as in the example above), accounting for saturation points, and more. Again, details can be found in the documentation, and I would highly encourage reading the white paper to gain a deeper understanding of the underlying methodologies.

That’s all we have time for, I hope this has been useful. Thanks for reading!