Time series analysis is a forecasting technique that builds on the idea that historical data can be used to project or predict future values, from which a trend or pattern can be discovered. For example, with linear regression in machine learning, using already existing data, the model (linear regression) can predict what the values of house prices will be within a preferred time frame, e.g, five years.
Facebook Prophet is a tool created by Facebook to address the challenge of forecasting for employees who work with data but might not be well-versed in the different forecasting techniques. It was developed to provide an option for analysts who need to perform forecasting as effectively as possible without extensive domain knowledge on the subject. There are a lot of forecasting techniques out there such as the ARIMA, SARIMA techniques. In this article the focus will be on Facebook Prophet exclusively. So, let’s get to work!
Installing Facebook Prophet is pretty straightforward.
Run !pip install pystan fbprophet
.
For an extensive approach to installation, check out this link provided by Facebook. https://pypi.org/project/fbprophet/
For the purpose of this article, the dataset that will be used is provided using this link https://github.com/JoshuaSmart-Olufemi/FB-Prophet-Dataset
The first thing to do is to import the Facebook Prophet module and the Pandas library, which will be very useful moving forward.
After loading the dataset using Pandas it should look like this:
Looking at the data types of the columns, the OrderDate column below happens to be an object. We need to change the data type to a datetime format so that Facebook Prophet can recognize the date column.
Make use of the to_datetime
function in Pandas to change the column format.
The first thing we’ll do is take note of the head and tail end of the dataset. This will be important for later.
The tail end of the date column has a date of 30th June, 2014. We will predict five years into the future based on this dataset to see what revenue values will look like.
Before training the model it is important to rename the OrderDate and Revenue columns. Within any dataset being used for FB forecasting there will be a need for y column and ds column. Ds will represent the date column, and y will represent the feature and its values that are being projected. This is necessary because the model is built to recognize these features labeled in this manner. This is a simple but crucial step that should never be ignored.
The next step is to instantiate the model by calling the model which has already been imported. Within this you can also specify confidence intervals for the data and the seasonality of the data, although this is something that doesn’t need to be specified as it won’t affect the model negatively.
Now we can go ahead and make predictions on the data.
In the image above, we’ve been able to forecast five years into the future from 2014. Periods refer to the number of days or weeks you want to forecast and freq represents the parameter/category with which you want to forecast, be it in months or years. In this case 1825 represents days over a five year period ( 365 *5) which is why freq is categorized under ‘D’ as days. The result of this code looks like this:
These are the top five rows of the new dataset. It goes without saying that trend refers to the trend of the data. Yhat_lower and yhat_upper represent the upper and lower estimates of what the projection will be.
There’s also the yhat at the end of the result set which represents the actual prediction itself. The bottom parts of this new dataset is gotten by using the forecast.tail()
function and looks like this:
As you can see, the bottom half of the dataset ends with date stamps that are in the year 2019 as opposed to our original dataset that ended in 2014.
With Facebook Prophet you can also create a visualization of the forecast easily with one line of code.
With another line of code you can create a visualization to look at the trend of the forecast along with a forecast on a daily, weekly and yearly level.
And that’s it! You’re done!
https://www.youtube.com/watch?v=KvLG1uTC-KU
Check out Topcoder Freelance Gigs.