How to extract seasonal trends from Prophet

Viewed 3544

I have been using Prophet from Facebook and so far it has been produced some great results.

Having looked in the docs and googling, there doesn't seem to be an automatic way to extract the seasonal trends from a model as a dataframe or a dict, e.g.:

weekly_trends = { 1 : monday_trend, 2 : tuesday_trend, ... , 7 : sunday_trend } 

yearly_trends = { 1 : day_1_trend, 2 : day_2_trend, ... , 365 : day_365_trend } 

Currently I can extract these out using a more manual way but was just wondering if I had missed something more elegant?

3 Answers

The trained model dataframe has all the seasonal, trend and holidays information. - take a look at its columns. Here's how to look into it in Python:

m = Prophet()
m.fit(ts)
future = m.make_future_dataframe()
forecast = m.predict(future)
print(forecast['weekly'])

Take any 7 days out of that series. That will give you the scale of the additive weekly adjustment for each weekday. Similar for the yearly seasonality.

You can specify daily, weekly, and yearly seasonality during model fit

    m = Prophet(changepoint_prior_scale=0.01, weekly_seasonality=False, holidays=holidays, interval_width=0.90, yearly_seasonality=True, mcmc_samples=300)
    m.add_seasonality(name='weekly', period=7, fourier_order=3)
    m.add_seasonality(name='monthly', period=30.5, fourier_order=8)
    m.fit(X)

When fitting the model, Prophet will save an OrderedDict that contains the seasonality.

You can retrieve them using m.seasonalities.

Example:

m = Prophet()
m.fit(ts)
print(m.seasonalities)

Will output something like this:

{
  "yearly": {
    "period": 365.25,
    "fourier_order": 10,
    "prior_scale": 10,
    "mode": "additive",
    "condition_name": null
  },
  "weekly": {
    "period": 7,
    "fourier_order": 3,
    "prior_scale": 10,
    "mode": "additive",
    "condition_name": null
  }
}

Than you can iterate the key (yearly, weekly, daily) and get the number of period or the other useful information.

Related