In those previous posts, I looked at forecasting monthly sales data 24 months into the future. In this post, I wanted to look at using the ‘holiday’ construct found within the Prophet library to try to better forecast around specific events. If we look at our sales data (you can find it here), there’s an obvious pattern each December. That pattern could be for a variety of reasons, but lets assume that its due to a promotion that is run every December. You can see the chart and pattern in the chart below.
Prophet allows you to build a holiday‘ dataframe and use that data in your modeling. For the purposes of this example, I’ll build my prophet holiday dataframe in the following manner:
This promotions dataframe consisists of promotion dates for Dec in 2009 through 2015, The lower_window and upper_window values are set to zero to indicate that we don’t want prophet to consider any other months than the ones listed.
With these steps, we’ve loaded the data, set it up the way prophet expects and ran our model with the promotions data and then plotted the model, which looks like the following:
Given that we have such little data, I doubt the use of holidays will make that much difference in the forecasts, but its a good example to use. We can check the difference in the model with holidays vs the model without by re-running the prophet forecast without holidays and see that the average difference between the two is ~ 0.06%…which isn’t terribly large, but still worth investigating. The jupyter notebook that accompanies this post goes into much more detail on this aspect (as well as the overall analysis).
Note: You can find the full code for this post in a Jupyter notebook here:
In previousposts, I described how I use Prophet to forecast time series data. There were some questions in the comments about the code not working, so I wanted to publish a new post with a link to a Jupyter Notebook that will hopefully provide a full, correct working example.
Note: There’s been some questions (and some issues with my original code). I’ve uploaded a jupyter notebook with corrected code for Part 1 and Part 2. The notebook can be found here.
In Forecasting Time-Series data with Prophet – Part 1, I introduced Facebook’s Prophet library for time-series forecasting. In this article, I wanted to take some time to share how I work with the data after the forecasts. Specifically, I wanted to share some tips on how I visualize the Prophet forecasts using matplotlib rather than relying on the default prophet charts (which I’m not a fan of).
For this work, we’ll need to import matplotlib and set up some basic parameters to be format our plots in a nice way (unlike the hideous default matplotlib format).
from fbprophet import Prophet
import numpy asnp
import pandas aspd
import matplotlib.pyplot asplt
%matplotlibinline#only needed for jupyter
With this chunk of code, we import fbprophet, numpy, pandas and matplotlib. Additionally, since I’m working in jupyter notebook, I want to add the %matplotlib inline instruction to view the charts that are created during the session. Lastly, I set my figuresize and sytle to use the ‘ggplot’ style.
Since I’ve already described the analysis phase with Prophet, I’m not going to provide commentary on it here. You can jump back to Part 1 for a walk-through.
sales_df['y_orig']=sales_df.y# We want to save the original data for later use
sales_df['y']=np.log(sales_df['y'])#take the log of the data to remove trends, etc
Now, let’s plot the output using Prophet’s built-in plotting capabilities.
While this is a nice chart, it is kind of ‘busy’ for me. Additionally, I like to view my forecasts with original data first and forecasts appended to the end (this ‘might’ make sense in a minute).
First, we need to get our data combined and indexed appropriately to start plotting. We are only interested (at least for the purposes of this article) in the ‘yhat’, ‘yhat_lower’ and ‘yhat_upper’ columns from the Prophet forecasted dataset. Note: There are much more pythonic ways to these steps, but I’m breaking them out for each of understanding.
ax1.set_title('Sales (Orange) vs Sales Forecast (Black)')
# change the legend text
L=ax1.legend()#get the legend
L.get_texts().set_text('Actual Sales')#change the legend text for 1st plot
L.get_texts().set_text('Forecasted Sales')#change the legend text for 2nd plot
This function does a few simple things. It finds the 2nd to last row of original data and then creates a new set of data (predict_df) with only the ‘future data’ included. It then creates a plot with confidence bands along the predicted data.
The ploit should look something like this:
Hopefully you’ve found some useful information here. Check back soon for Part 3 of my Forecasting Time-Series data with Prophet.
If you'd like to receive updates when new posts are published, signup for my mailing list. I won't sell or share your email.