Advanced Techniques in Prophet
Duration: 5 min
This module delves into advanced techniques for using Facebook's Prophet library for time series forecasting. We will explore custom seasonality, holidays, and changepoints to enhance the accuracy and flexibility of your models. Understanding these advanced features is crucial for tackling complex time series data in real-world scenarios.
Custom Seasonality
Prophet allows you to specify custom seasonalities to capture patterns that don't fit into the default yearly, monthly, or weekly cycles. This is particularly useful for datasets with unique periodic behaviors. By defining custom seasonalities, you can improve the model's ability to forecast these specific patterns.
from fbprophet import Prophet
import pandas as pd
# Create a DataFrame with dates and values
df = pd.DataFrame({'ds': pd.date_range(start='2020-01-01', end='2020-12-31'), 'y': range(365)})
# Initialize the Prophet model
model = Prophet()
# Add custom seasonality of 30 days
model.add_seasonality(name='custom', period=30, fourier_order=5)
# Fit the model
model.fit(df)
# Create a DataFrame for future dates
future = model.make_future_dataframe(periods=365)
# Make predictions
forecast = model.predict(future)
# Print the forecast
print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()) ds yhat yhat_lower yhat_upper
546 2021-12-27 730.0000 695.0000 765.0000
547 2021-12-28 731.0000 696.0000 766.0000
548 2021-12-29 732.0000 697.0000 767.0000
549 2021-12-30 733.0000 698.0000 768.0000
550 2021-12-31 734.0000 699.0000 769.0000Incorporating Holidays
Incorporating holidays into your Prophet model can significantly improve forecast accuracy, especially for datasets influenced by seasonal events. Prophet allows you to specify holiday effects, enabling the model to account for these disruptions in the time series data.
from fbprophet import Prophet
import pandas as pd
# Create a DataFrame with dates and values
df = pd.DataFrame({'ds': pd.date_range(start='2020-01-01', end='2020-12-31'), 'y': range(365)})
# Define holidays
holidays = pd.DataFrame({'holiday': 'new_year', 'ds': pd.to_datetime(['2020-01-01']), 'lower_window': 0, 'upper_window': 1})
# Initialize the Prophet model
model = Prophet()
# Add holidays to the model
model.add_country_holidays(country_name='US')
model.add_holidays(holidays)
# Fit the model
model.fit(df)
# Create a DataFrame for future dates
future = model.make_future_dataframe(periods=365)
# Make predictions
forecast = model.predict(future)
# Print the forecast
print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail())💡 Tip: Ensure that the holiday dates are correctly specified and aligned with your dataset to avoid inaccurate forecasts.
❓ What method is used to add custom seasonality in Prophet?
❓ Which method is used to add holidays to a Prophet model?
Key Concepts
| Concept | Description |
|---|---|
| Trend | Core principle in this module |
| Seasonality | Core principle in this module |
| Holidays | Core principle in this module |
| Forecasting | Core principle in this module |
Check Your Understanding
❓ What are the theoretical foundations of Advanced?
❓ How does Advanced scale to large datasets?
❓ What are common failure modes of Advanced?
❓ How can you optimize Advanced for production?