Skip to content

Instantly share code, notes, and snippets.

@christinebuckler
Created April 1, 2024 21:22
Show Gist options
  • Save christinebuckler/0a711b3f8b24b823344514328d440de7 to your computer and use it in GitHub Desktop.
Save christinebuckler/0a711b3f8b24b823344514328d440de7 to your computer and use it in GitHub Desktop.
decompose the trend and seasonality components of a time-series forecast
# https://towardsdatascience.com/time-series-forecasting-based-on-the-trend-and-seasonal-components-26b92866e548
from statsmodels.tsa.seasonal import seasonal_decompose
def decompose(df):
decomposition = sm.tsa.seasonal_decompose(df, model='additive', freq=365)
trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid
fig = decomposition.plot()
fig.set_size_inches(14, 7)
plt.show()
return trend, seasonal, residual
components = decompose(df['Retail_Sales'], model='multiplicative')
components.plot()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment