# -*- coding: utf-8 -*- """Prophet_Tutorial.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/16gcE403VP2pv_t1xQgxMRda9QKebk7vF """ pip install prophet from prophet import Prophet import pandas as pd df_data = pd.read_csv("https://raw.githubusercontent.com/jonasdieckmann/prophet_tutorial/main/passengers.csv") df_data_train = df_data[df_data["Month"] < "2023-01"] df_data_test = df_data[df_data["Month"] >= "2023-01"] display(df_data_train) from statsmodels.tsa.seasonal import seasonal_decompose decompose = seasonal_decompose(df_data_train.Passengers, model='additive', extrapolate_trend='freq', period=12) decompose.plot().show() # date variable needs to be named "ds" for prophet df_train_prophet = df_data_train df_train_prophet = df_train_prophet.rename(columns={"Month": "ds"}) # target variable needs to be named "y" for prophet df_train_prophet = df_train_prophet.rename(columns={"Passengers": "y"}) model_prophet = Prophet() model_prophet.fit(df_train_prophet) df_future = model_prophet.make_future_dataframe(periods=12, freq='MS') display(df_future) forecast_prophet = model_prophet.predict(df_future) forecast_prophet[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].round().tail() import matplotlib.pyplot as plt # plot the time series forecast_plot = model_prophet.plot(forecast_prophet) # add a vertical line at the end of the training period axes = forecast_plot.gca() last_training_date = forecast_prophet['ds'].iloc[-12] axes.axvline(x=last_training_date, color='red', linestyle='--', label='Training End') # plot true test data for the period after the red line df_data_test['Month'] = pd.to_datetime(df_data_test['Month']) plt.plot(df_data_test['Month'], df_data_test['Passengers'],'ro', markersize=3, label='True Test Data') # show the legend to distinguish between the lines plt.legend()