Air Traffic Forecasting Using Facebook’s Prophet in Python (Time-Series Data)

Vijay Vankayalapati
4 min readJun 10, 2021

Prophet is an easy-to-use library to predict time-series data

Overview:

Prophet is a python library developed by Facebook to simplify forecasting of univariate time-series data. It was created for internal use in the company for predicting sales, compute etc. and later open sourced.

It was designed to completely automate the task of predicting times-series data by factoring in parameters like seasonality, trends and holidays. For detailed understanding, please refer to official documentation and here.

Exploring the dataset:

In this article, we will analyse Air Passengers dataset and forecast the future data using prophet. First download the dataset from here.The dataset contains monthly air passengers in USA data from 1949 to 1960.

Install the required packages and load the csv file from your path.

from pandas import read_csv
from matplotlib import pyplot
path = '/content/drive/My Drive/Facebook Prop/AirPassengers.csv'
df = read_csv(path)
df.head()

Execute the following function to get details of the csv file. We can see it has 144 rows and two columns — Month and #Passengers.

df.info()

Let’s plot the data to get a sense it.

df.plot()
pyplot.show()

Observe the trends in the graph overtime and same should be predicted for future using prophet.

Fitting the model:

Our model will be defined as an instance of Prophet() and fit on the dataset using the function fit().

Prophet() takes the arguments to factor in type of growth, type of seasonality etc. but even if no arguments are given, it mostly figures out everything automatically.

The fit() function takes dataframe as an object but in a specific format. The first column should be named ‘ds‘ and it contains the date-times. The second column should be named ‘y‘ and it contains the values.

So we rename the columns accordingly and convert the first column values to date-time objects.

from pandas import to_datetimedf.columns = ['ds', 'y']
df['ds']= to_datetime(df['ds'])

Fitting our model to the dataset:

from fbprophet import Prophetmodel = Prophet()
model.fit(df)

Weekly and daily seasonality are disabled since our data is of monthly frequency. But we can get a sense of monthly trend and the general trend overall using the following code.

model.plot_components(model.predict(df))

The above figures indicate the number of passengers travelled is generally increasing and it peaks around July every year.

Forecast on in-sample data:

In-sample forecast gives a good idea about how the model performed on training data. It is achieved by fitting predict() function on the last 12 months of our data (using slicing on dataframe).

forecast = model.predict(df[132:])
print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].head())

We shall now plot our prediction alongside the training data to see if our predictions are close to the actual values.

model.plot(forecast)
pyplot.show()

In the above graph, the black dots refer to training data and blue line is our forecast for last 12 months. The blue shaded area is upper and lower bounds. It can be inferred that our predictions for the last 12 months are close to the actual values in the dataset.

Forecast on out-of-sample data:

In this section, we shall predict air traffic for the next five years.

future = model.make_future_dataframe(periods=12 * 5, freq='M')
forecast = model.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper', 'trend', 'trend_lower', 'trend_upper']].tail()

Plotting the same:

model.plot(forecast)
pyplot.show()

From the graph we can conclude the prediction looks inline with the trends observed for the past data. So our model did a good job.

Mean Absolute Error:

We evaluate our model manually by calculating mean absolute error (MAE). This can be done by holding back last few months of data during training and predict for the same duration using our model. As the values for the last 12 months are already known to us, we can calculate the error between the predictions and actual values.

model = Prophet()
model.fit(df[:132]) #excluding last 12 rows
forecast = model.predict(df[132:])y_true = df['y'][132:].values
y_pred = forecast['yhat'].values
from sklearn.metrics import mean_absolute_errormae = mean_absolute_error(y_true, y_pred)
print('MAE: %.2f' % mae)
pyplot.plot(y_true, label='Actual')
pyplot.plot(y_pred, label='Predicted')
pyplot.legend()
pyplot.show()

It is evident that our model is good enough to capture the trend in the dataset. MAE is also within acceptable limits.

We can conclude Prophet is good tool to easily predict univariate time-series data.

Colab notebook can be found here.

--

--