Complete the code to extract the trend component from a time series using moving average.
trend = time_series.rolling(window=[1]).mean()The moving average window size of 3 smooths short-term fluctuations to reveal the trend.
Complete the code to decompose a time series into trend, seasonality, and residual using statsmodels.
from statsmodels.tsa.seasonal import seasonal_decompose result = seasonal_decompose(time_series, model=[1], period=12)
The 'additive' model assumes components add together: observed = trend + seasonality + residual.
Fix the error in the code to plot the seasonal component after decomposition.
import matplotlib.pyplot as plt result = seasonal_decompose(time_series, model="additive", period=12) plt.plot(result.[1]) plt.show()
The seasonal component is accessed with result.seasonal to plot seasonality.
Fill both blanks to create a dictionary of time series components with keys 'trend' and 'seasonality'.
components = {"trend": result.[1], "seasonality": result.[2]The trend and seasonal components are accessed via result.trend and result.seasonal.
Fill all three blanks to compute the residual component by subtracting trend and seasonality from the original series.
residual = time_series - result.[1] - result.[2] print(residual.[3]())
Residual is original minus trend and seasonal. Use dropna() to remove missing values after subtraction.