0
0
ML Pythonml~10 mins

Time series components (trend, seasonality) in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to extract the trend component from a time series using moving average.

ML Python
trend = time_series.rolling(window=[1]).mean()
Drag options to blanks, or click blank then click option'
A12
B1
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using window size 1 does not smooth the data.
Using window size 0 causes an error.
2fill in blank
medium

Complete the code to decompose a time series into trend, seasonality, and residual using statsmodels.

ML Python
from statsmodels.tsa.seasonal import seasonal_decompose
result = seasonal_decompose(time_series, model=[1], period=12)
Drag options to blanks, or click blank then click option'
A"additive"
B"multiplicative"
C"linear"
D"exponential"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'multiplicative' when seasonal effects are not proportional.
Using unsupported model types like 'linear'.
3fill in blank
hard

Fix the error in the code to plot the seasonal component after decomposition.

ML Python
import matplotlib.pyplot as plt
result = seasonal_decompose(time_series, model="additive", period=12)
plt.plot(result.[1])
plt.show()
Drag options to blanks, or click blank then click option'
Aseasonal
Btrend
Cobserved
Dresidual
Attempts:
3 left
💡 Hint
Common Mistakes
Plotting trend or residual instead of seasonal component.
Using an attribute that does not exist.
4fill in blank
hard

Fill both blanks to create a dictionary of time series components with keys 'trend' and 'seasonality'.

ML Python
components = {"trend": result.[1], "seasonality": result.[2]
Drag options to blanks, or click blank then click option'
Atrend
Bseasonal
Cresidual
Dobserved
Attempts:
3 left
💡 Hint
Common Mistakes
Using residual or observed instead of trend or seasonal.
Mixing up keys and values.
5fill in blank
hard

Fill all three blanks to compute the residual component by subtracting trend and seasonality from the original series.

ML Python
residual = time_series - result.[1] - result.[2]
print(residual.[3]())
Drag options to blanks, or click blank then click option'
Atrend
Bseasonal
Cdropna
Dfillna
Attempts:
3 left
💡 Hint
Common Mistakes
Using fillna() instead of dropna(), which fills missing values instead of removing them.
Mixing up trend and seasonal in subtraction.