pythonintermediate

Prophet Time Series Forecasting

Forecast time-series data with Facebook Prophet handling holidays, trends, and seasonality.

python
from prophet import Prophet
import pandas as pd
import numpy as np

# Generate sample time series
dates = pd.date_range('2023-01-01', periods=365, freq='D')
trend = np.linspace(100, 150, 365)
seasonality = 10 * np.sin(2 * np.pi * np.arange(365) / 365)
noise = np.random.normal(0, 3, 365)

df = pd.DataFrame({'ds': dates, 'y': trend + seasonality + noise})

model = Prophet(yearly_seasonality=True, weekly_seasonality=True, daily_seasonality=False, seasonality_mode='multiplicative')
model.fit(df)

future = model.make_future_dataframe(periods=90)
forecast = model.predict(future)

future_only = forecast[forecast['ds'] > df['ds'].max()]
print(forecast[['ds','yhat','yhat_lower','yhat_upper']].tail(5).to_string(index=False))

Use Cases

  • sales forecasting
  • traffic prediction
  • demand planning

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.