Forecasting with Prophet
2 mins
Prophet and Key Data Assumptions #
Prophet is a python library released by Facebook’s data science team for time series forecasting that is based on an additive model where non-linear trends are fit with daily, weekly, and yearly seasonality. It is a very nifty tool but it does come with several assumptions that are worth outlining first:
- Your time series is stationary - meaning the mean and variance do not change over time.
- Your time series has a linear or logistic trend and that the seasonality is addictive or multiplicative.
- You have a reasonable amount of historical data. Typically one year of daily data or two years of weekly data.
- Your time series is not affected by external factors such as economic factors or customer behavior.
- Your time series is not affected by outliers or missing data. If you have either, they must be handled before your Prophet.
Example #
Below is an example demonstrating the basic process of using Prophet with python to forecast a time series dataset. In this example, we’ll use prophet alongside pandas with the sample data provided by prophet.
Import the necessary libraries. #
import pandas as pd
from prophet import prophet
Import example data. #
data = pd.read_csv('https://raw.githubusercontent.com/facebook/prophet/main/examples/example_wp_log_peyton_manning.csv')
Create a Prophet model. Add additional seasonal parameters, if necessary like weekly or yearly seasonality. #
model = Prophet(seasonality_mode='additive',
weekly_seasonality=True,
yearly_seasonality=True,
)
Fit the model to you data. #
model.fit(data)
Make predictions for future dates. In this case, we are forecasting 365 days into the future. #
future = model.make_future_dataframe(periods=365)
Generate the forecast. #
forecast = model.predict(future)
Visualize the generated forecast. #
model.plot(forecast)
Visualize a more detailed forecast components. #
model.plot_components(forecast)