Univariate Time Series Forecasting using FBProphet

Soubhik Khankary
4 min readJan 29, 2022

--

Today we would be discussing about one of the easiest way to develop a model which would help us to forecast a time series for an univariate variable using a library provided extensively by Facebook i.e. FBProphet. I feel it is one of the robust and easiest way to develop the model for forecasting. Seems like, it is one of the easiest way just that we have to follow up some process which I would be showing you below.

In this particular post we will try to consider a dataset which has got yearly trend and we will try to train our model on existing dataset and then predict the forecast for future dates. Let me show you the sample dataset and how it looks like.

Top Head Data
Tail Dataset

The above data consists the information for sales of cycles that has occurred each month from year 1960- Jan to 1968-Dec. I am planning to split the dataset into training and testing dataset where I will try to train the model for my dataset from year 1960-Jan to 1967-Dec and try to forecast the value of sales for cycle in year 1968. It is because it will help me out to compare the predicted value with actual values and understand if my model is working correctly or not. Hope it sounds good . Let us dive deep into the coding stuffs and understand how it could be implemented so quickly.

Let’s try to import the very basic modules:

Importing the basic modules

Reading the csv and splitting the dataset into train and test dataset. As our main focus is to learn the implementation of FBProphet, it is why I have chosen a simple dataset where I don’t have to deal with cleaning and standardization of the dataset. As planned 96 records comprises of 8 years dataset and will be used to train our model which in turn would be used for predicting the sales in year 1968.

Checking the shapes of the dataframes and have a look and feel of it.

A small glance of the test data frame.

An important step that needs to be taken care. The Date column has to be converted to datetime in pandas data frame. Then the date column has to be renamed to ds and the column that has to be forecasted for could be renamed as y. It is mandatory and has to be followed if you want to use model FBProphet.

Ignore the warnings please :-)
Renamed my column as discussed

Plotting the graph for the value we are trying to forecast to understand the seasonal trend .

predicted with time value

Looking at the graph we could see the trend is going upwards and seasonality is yearly in nature.

Let’s do apply the FBProphet model by switch on the yearly trend as True.

Our model is trained and now we have to create a data frame on which we have to apply our model. The data frame must consist only the forecasted dates with column named as ‘ds’. Hope you get it .

Looks Good the date columns were extracted for each month

Use the same model to predict the sales value for the new forecasted dates. Here it is how it has to be done:

ds-represents the monthly values for year 1968, yhat-predicted values for sales

Try to merge two different data frames which would help us to compare the actual and predicted values.

Actual Test Dataframe

Try to merge the predicted and actual data frame to check their corresponding values side by side. Let’s view it :

Let’s visualize the sales value for each month in year 1968.

My blue follows Red very well except in few unexceptional spikes

Hope , you liked my blog of solving univariate time series problem using FB Prophet package. In case if you want to provide me feedback please do provide it in comments. Thank You !!!

I will be posting Multivariate Time series Forecasting using FBProphet soon.

--

--

Soubhik Khankary

Data Engineer by job , Teaching computers by stats and love to learn never endless math.