Created
April 1, 2024 21:22
-
-
Save christinebuckler/0a711b3f8b24b823344514328d440de7 to your computer and use it in GitHub Desktop.
decompose the trend and seasonality components of a time-series forecast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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