Time series components help us understand patterns in data collected over time. They show us the main directions and repeating cycles in the data.
Time series components (trend, seasonality) in ML Python
Start learning this pattern below
Jump into concepts and practice - no test required
Trend: The long-term increase or decrease in data. Seasonality: Regular repeating patterns over fixed periods.
Trend shows the overall direction, like a steady rise or fall.
Seasonality repeats at regular intervals, like daily, weekly, or yearly cycles.
Trend: Sales slowly increasing every month. Seasonality: More sales every December.
Trend: Temperature rising over decades. Seasonality: Temperature peaks every summer.
This code creates a fake time series with a clear trend and seasonality. It then breaks the data into parts and shows them with plots and prints.
import numpy as np import matplotlib.pyplot as plt from statsmodels.tsa.seasonal import seasonal_decompose # Create time series data with trend and seasonality np.random.seed(0) time = np.arange(100) trend = time * 0.1 # slowly increasing trend seasonal = 10 * np.sin(2 * np.pi * time / 20) # repeating pattern every 20 steps noise = np.random.normal(scale=1, size=100) data = trend + seasonal + noise # Decompose the time series result = seasonal_decompose(data, model='additive', period=20) # Plot components plt.figure(figsize=(10,8)) plt.subplot(411) plt.plot(data) plt.title('Original Data') plt.subplot(412) plt.plot(result.trend) plt.title('Trend Component') plt.subplot(413) plt.plot(result.seasonal) plt.title('Seasonal Component') plt.subplot(414) plt.plot(result.resid) plt.title('Residual (Noise)') plt.tight_layout() plt.show() # Print first 5 values of each component print('First 5 trend values:', result.trend[:5]) print('First 5 seasonal values:', result.seasonal[:5]) print('First 5 residual values:', result.resid[:5])
Trend values at the edges may be NaN because of how the method calculates moving averages.
Seasonality repeats exactly every set period (20 here), showing the repeating pattern.
Residual is what is left after removing trend and seasonality, often noise.
Time series data can be split into trend, seasonality, and residual parts.
Trend shows the overall direction over time.
Seasonality shows repeating cycles at fixed intervals.
Practice
Solution
Step 1: Understand the meaning of trend
The trend component represents the overall direction or pattern in the data over a long period, such as increasing sales over years.Step 2: Differentiate from seasonality and noise
Seasonality repeats in fixed cycles (like monthly), and noise is random variation. Trend is the smooth long-term movement.Final Answer:
Trend -> Option AQuick Check:
Long-term direction = Trend [OK]
- Confusing seasonality with trend
- Thinking noise is trend
- Mixing residual with trend
Solution
Step 1: Identify how to extract seasonality
Seasonality repeats in fixed intervals like months, so grouping by month and averaging shows seasonal pattern.Step 2: Check code options
df['value'].groupby(df.index.month).mean().plot() groups by month and plots mean, revealing seasonality. Others plot raw data, trend (rolling mean), or differences.Final Answer:
df['value'].groupby(df.index.month).mean().plot() -> Option CQuick Check:
Group by time period for seasonality plot [OK]
- Plotting raw data only
- Using rolling mean for seasonality
- Plotting differences instead of seasonal groups
seasonal?
import pandas as pd
import numpy as np
index = pd.date_range('2023-01-01', periods=12, freq='M')
data = np.sin(np.linspace(0, 2 * np.pi, 12))
df = pd.Series(data, index=index)
seasonal = df.groupby(df.index.month).transform('mean')Solution
Step 1: Understand groupby with transform
Using groupby with transform('mean') returns a Series aligned with original index, same length as df.Step 2: Check output type
Since df is a Series, seasonal is also a Series with same length, each value replaced by group mean.Final Answer:
A pandas Series with same length as df -> Option BQuick Check:
groupby + transform returns Series matching original length [OK]
- Thinking transform returns single value
- Confusing transform with aggregate
- Expecting DataFrame instead of Series
trend = df['value'].rolling(window=3).mean()But the output has many NaN values at the start. How can you fix this?
Solution
Step 1: Understand rolling mean NaNs
Rolling mean with window=3 needs 3 values to compute, so first 2 are NaN by default.Step 2: Use min_periods to allow fewer values
Setting min_periods=1 lets rolling mean compute with fewer points, reducing NaNs at start.Final Answer:
Use min_periods=1 in rolling to reduce NaNs -> Option DQuick Check:
min_periods controls minimum data points for rolling [OK]
- Changing window to 1 loses smoothing
- Dropping NaNs loses early data
- Using diff() does not fix NaNs
Solution
Step 1: Understand yearly seasonality and trend
Yearly seasonality repeats every 12 months; trend is slow upward movement.Step 2: Choose method to separate components
Moving average with window=12 smooths out seasonality, capturing trend. Subtracting trend leaves seasonality.Step 3: Evaluate other options
Differencing with lag=1 removes short-term changes, not yearly seasonality. Fourier transform is complex. Rolling mean with window=3 is too short for yearly seasonality.Final Answer:
Use moving average with window=12 for trend, then subtract to get seasonality -> Option AQuick Check:
Window matches season length to isolate trend [OK]
- Using too short window for moving average
- Confusing differencing lag with season length
- Ignoring trend when extracting seasonality
