Prophet is an open-source forecasting library developed by Facebook (now Meta). It was designed to be easy to use and to produce reliable forecasts for business time series data, which often have strong seasonal effects and holiday impacts.
Why Use Prophet?
- User-Friendly: Prophet's API is intentionally simple. You don't need to be a time series expert to get started.
- Handles Real-World Data: It's robust to missing data, shifts in trends, and outliers.
- Automatic Seasonality: It automatically detects yearly, weekly, and daily seasonality.
- Customizable: You can easily add custom holidays and additional regressors.
Prophet works best with time series that have strong seasonal effects and several seasons of historical data.
The Prophet Data Format
The most important step in using Prophet is formatting your data correctly. Prophet expects a pandas DataFrame with two specific column names:
- ds: This column must contain the dates or timestamps. It should be of a datetime type.
- y: This column must contain the numeric values we want to forecast.
All other columns will be ignored unless they are added as extra regressors.
A Quickstart Forecasting Example
Let's walk through a complete forecasting pipeline in just a few lines of code.
First, install the library:
Bash
# Using pip pip install prophet # Using conda conda install -c conda-forge prophet
Now, let's create a forecast.
Python
import pandas as pd
from prophet import Prophet
from prophet.plot import plot_plotly
# 1. Prepare the data
# Let's create a sample DataFrame in the required format
data = {
'ds': pd.to_datetime(pd.date_range(start='2022-01-01', periods=365, freq='D')),
'y': [10 + i/30 + 5 * (pd.to_datetime(d).weekday() > 4) + 10 * (pd.to_datetime(d).month in [6,7,8]) for i, d in enumerate(pd.date_range(start='2022-01-01', periods=365, freq='D'))]
}
df = pd.DataFrame(data)
# 2. Instantiate and fit the model
# Prophet follows the scikit-learn API convention
model = Prophet()
model.fit(df)
# 3. Create a future DataFrame for predictions
# The `make_future_dataframe` method makes this easy
future = model.make_future_dataframe(periods=90) # Forecast for the next 90 days
# 4. Make predictions
# The `predict` method returns a DataFrame with the forecast and its components
forecast = model.predict(future)
# The forecast object contains many columns, but the most important are:
# 'ds': The timestamp
# 'yhat': The forecasted value
# 'yhat_lower': The lower bound of the uncertainty interval
# 'yhat_upper': The upper bound of the uncertainty interval
print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail())
# 5. Plot the results
# Prophet has built-in plotting functions
fig1 = model.plot(forecast)
fig2 = model.plot_components(forecast)
# For interactive plots, you can use plot_plotly
# fig3 = plot_plotly(model, forecast)
# if fig3 is not None:
# fig3.show()
And that's it! In just a few lines, you have a robust forecast that includes the trend, weekly seasonality, and yearly seasonality, complete with uncertainty intervals. Prophet's simplicity and power make it an excellent first choice for many common forecasting tasks.