Download as pdf or txt
Download as pdf or txt
You are on page 1of 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