0% found this document useful (0 votes)
109 views

Forecasting Using Facebook's Prophet Library

This document demonstrates how to use the Prophet library for time series forecasting. It loads airline passenger data, trains a Prophet forecasting model on historical data, generates forecasts for 12 future time periods, and evaluates the accuracy of the forecasts using RMSE. Key steps include defining the training and test sets, fitting the Prophet model to the training data, making predictions, and calculating the RMSE between predicted and actual values in the test set.

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Forecasting Using Facebook's Prophet Library

This document demonstrates how to use the Prophet library for time series forecasting. It loads airline passenger data, trains a Prophet forecasting model on historical data, generates forecasts for 12 future time periods, and evaluates the accuracy of the forecasts using RMSE. Key steps include defining the training and test sets, fitting the Prophet model to the training data, making predictions, and calculating the RMSE between predicted and actual values in the test set.

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Forecasting Using Facebook’s Prophet Library | by Christoph... https://medium.com/analytics-vidhya/forecasting-using-face...

1 of 11 11/8/2021, 2:24 PM
Forecasting Using Facebook’s Prophet Library | by Christoph... https://medium.com/analytics-vidhya/forecasting-using-face...

2 of 11 11/8/2021, 2:24 PM
Forecasting Using Facebook’s Prophet Library | by Christoph... https://medium.com/analytics-vidhya/forecasting-using-face...

from google.colab import drive

drive.mount(‘/content/drive’, force_remount=True)

from fbprophet import Prophet


import pandas as pd

3 of 11 11/8/2021, 2:24 PM
Forecasting Using Facebook’s Prophet Library | by Christoph... https://medium.com/analytics-vidhya/forecasting-using-face...

df = pd.read_csv(‘../datasets/air_passenger.csv’)

df.head()

df.columns = [‘ds’, ‘y’]

4 of 11 11/8/2021, 2:24 PM
Forecasting Using Facebook’s Prophet Library | by Christoph... https://medium.com/analytics-vidhya/forecasting-using-face...

df[‘ds’] = pd.to_datetime(df[‘ds’])

# defining the number of observations we want to


predict
nobs = 12

train = df[:-nobs]
test = df[-nobs:]

print(f"Length of dataframe: {len(df)}\n"


f"Length of train set: {len(train)}\n"
f"Length of test set: {len(test)}")

5 of 11 11/8/2021, 2:24 PM
Forecasting Using Facebook’s Prophet Library | by Christoph... https://medium.com/analytics-vidhya/forecasting-using-face...

# Creating an instance of the Prophet model


prophet = Prophet()

# fitting Prophet model to the train set


prophet.fit(train)

future = prophet.make_future_dataframe(periods=nobs,
freq=’MS’)

6 of 11 11/8/2021, 2:24 PM
Forecasting Using Facebook’s Prophet Library | by Christoph... https://medium.com/analytics-vidhya/forecasting-using-face...

forecast = prophet.predict(future)

fig1 = prophet.plot(forecast)

from fbprophet.plot import add_changepoint_to_plot

fig1 = prophet.plot(forecast)

# viewing the points in time where the trajectory of


the price index changed
a = add_changepoints_to_plot(fig1.gca(), prophet,

7 of 11 11/8/2021, 2:24 PM
Forecasting Using Facebook’s Prophet Library | by Christoph... https://medium.com/analytics-vidhya/forecasting-using-face...

forecast)

8 of 11 11/8/2021, 2:24 PM
Forecasting Using Facebook’s Prophet Library | by Christoph... https://medium.com/analytics-vidhya/forecasting-using-face...

from statsmodels.tools.eval_measures import rmse

# Remember nobs = 12

9 of 11 11/8/2021, 2:24 PM
Forecasting Using Facebook’s Prophet Library | by Christoph... https://medium.com/analytics-vidhya/forecasting-using-face...

y_pred = forecast.iloc[-nobs:][‘yhat’]
y_true = test['y']

rmse(y_pred, y_true)

10 of 11 11/8/2021, 2:24 PM
Forecasting Using Facebook’s Prophet Library | by Christoph... https://medium.com/analytics-vidhya/forecasting-using-face...

11 of 11 11/8/2021, 2:24 PM

You might also like