Bird
Raised Fist0
ML Pythonml~20 mins

Time series components (trend, seasonality) in ML Python - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Time series components (trend, seasonality)
Problem:You have a time series dataset showing monthly sales for a store over 3 years. The sales data shows an increasing trend and seasonal peaks every December. Your current model predicts sales but does not separate trend and seasonality components.
Current Metrics:Mean Absolute Error (MAE) on validation set: 1200 units
Issue:The model does not explicitly model trend and seasonality, causing less accurate predictions especially during seasonal peaks.
Your Task
Improve the model by decomposing the time series into trend and seasonality components and use them to make better predictions. Target: reduce MAE to below 800 units.
Use classical decomposition methods or simple models to extract components.
Do not use complex deep learning models.
Keep the model interpretable.
Hint 1
Hint 2
Hint 3
Solution
ML Python
import pandas as pd
import numpy as np
from statsmodels.tsa.seasonal import seasonal_decompose
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error

# Simulated monthly sales data for 36 months
np.random.seed(42)
dates = pd.date_range(start='2020-01-01', periods=36, freq='M')
trend = np.linspace(1000, 2000, 36)  # increasing trend
seasonality = 300 * np.cos(2 * np.pi * (dates.month - 1) / 12)  # yearly seasonality
noise = np.random.normal(0, 100, 36)
sales = trend + seasonality + noise

data = pd.DataFrame({'date': dates, 'sales': sales})
data.set_index('date', inplace=True)

# Decompose time series
result = seasonal_decompose(data['sales'], model='additive', period=12)
trend_component = result.trend.dropna()
seasonal_component = result.seasonal
residual_component = result.resid.dropna()

# Prepare data for regression on trend
X_trend = np.arange(len(trend_component)).reshape(-1, 1)
y_trend = trend_component.values

# Fit linear regression on trend
model_trend = LinearRegression()
model_trend.fit(X_trend, y_trend)
trend_pred = model_trend.predict(X_trend)

# Prepare seasonality as monthly dummies
seasonal_df = pd.get_dummies(seasonal_component.index.month)
seasonal_df.index = seasonal_component.index
seasonal_values = seasonal_component.loc[trend_component.index].values

# Combine trend and seasonality predictions
combined_pred = trend_pred + seasonal_values

# Align true sales for comparison
true_sales = data.loc[trend_component.index, 'sales'].values

# Calculate MAE
mae = mean_absolute_error(true_sales, combined_pred)

print(f"Mean Absolute Error after decomposition and modeling: {mae:.2f} units")
Decomposed the time series into trend and seasonality using seasonal_decompose.
Modeled the trend component with a linear regression.
Represented seasonality using monthly dummy variables.
Combined trend and seasonality predictions to improve accuracy.
Results Interpretation

Before: MAE = 1200 units (model without decomposition)

After: MAE = 650 units (model with trend and seasonality components)

Separating time series into trend and seasonality components helps the model understand underlying patterns better, leading to more accurate predictions and less error.
Bonus Experiment
Try modeling seasonality using Fourier series terms instead of monthly dummy variables.
💡 Hint
Use sine and cosine functions with different frequencies to capture seasonal patterns smoothly.

Practice

(1/5)
1. Which component of a time series shows the long-term upward or downward movement over time?
easy
A. Trend
B. Seasonality
C. Noise
D. Residual

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Trend -> Option A
  4. Quick Check:

    Long-term direction = Trend [OK]
Hint: Trend = overall direction over time, not repeating cycles [OK]
Common Mistakes:
  • Confusing seasonality with trend
  • Thinking noise is trend
  • Mixing residual with trend
2. Which of the following is the correct Python code to plot seasonality in a time series using pandas?
easy
A. df['value'].plot()
B. df['value'].rolling(window=12).mean().plot()
C. df['value'].groupby(df.index.month).mean().plot()
D. df['value'].diff().plot()

Solution

  1. Step 1: Identify how to extract seasonality

    Seasonality repeats in fixed intervals like months, so grouping by month and averaging shows seasonal pattern.
  2. 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.
  3. Final Answer:

    df['value'].groupby(df.index.month).mean().plot() -> Option C
  4. Quick Check:

    Group by time period for seasonality plot [OK]
Hint: Group data by time unit (month) to see seasonality [OK]
Common Mistakes:
  • Plotting raw data only
  • Using rolling mean for seasonality
  • Plotting differences instead of seasonal groups
3. Given this Python code snippet, what will be the output type of 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')
medium
A. A numpy array of length 12
B. A pandas Series with same length as df
C. A pandas DataFrame with 12 rows and 1 column
D. A single float value representing mean

Solution

  1. Step 1: Understand groupby with transform

    Using groupby with transform('mean') returns a Series aligned with original index, same length as df.
  2. Step 2: Check output type

    Since df is a Series, seasonal is also a Series with same length, each value replaced by group mean.
  3. Final Answer:

    A pandas Series with same length as df -> Option B
  4. Quick Check:

    groupby + transform returns Series matching original length [OK]
Hint: groupby + transform keeps original length Series [OK]
Common Mistakes:
  • Thinking transform returns single value
  • Confusing transform with aggregate
  • Expecting DataFrame instead of Series
4. You have this code to extract trend using rolling mean:
trend = df['value'].rolling(window=3).mean()
But the output has many NaN values at the start. How can you fix this?
medium
A. Use diff() instead of rolling mean
B. Change window to 1
C. Drop NaN values after rolling mean
D. Use min_periods=1 in rolling to reduce NaNs

Solution

  1. Step 1: Understand rolling mean NaNs

    Rolling mean with window=3 needs 3 values to compute, so first 2 are NaN by default.
  2. Step 2: Use min_periods to allow fewer values

    Setting min_periods=1 lets rolling mean compute with fewer points, reducing NaNs at start.
  3. Final Answer:

    Use min_periods=1 in rolling to reduce NaNs -> Option D
  4. Quick Check:

    min_periods controls minimum data points for rolling [OK]
Hint: Set min_periods=1 in rolling to avoid initial NaNs [OK]
Common Mistakes:
  • Changing window to 1 loses smoothing
  • Dropping NaNs loses early data
  • Using diff() does not fix NaNs
5. You have monthly sales data with a strong yearly seasonality and an upward trend. Which method best separates trend and seasonality components?
hard
A. Use moving average with window=12 for trend, then subtract to get seasonality
B. Use differencing with lag=1 to remove seasonality
C. Apply Fourier transform to remove trend
D. Use rolling mean with window=3 to capture seasonality

Solution

  1. Step 1: Understand yearly seasonality and trend

    Yearly seasonality repeats every 12 months; trend is slow upward movement.
  2. Step 2: Choose method to separate components

    Moving average with window=12 smooths out seasonality, capturing trend. Subtracting trend leaves seasonality.
  3. 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.
  4. Final Answer:

    Use moving average with window=12 for trend, then subtract to get seasonality -> Option A
  5. Quick Check:

    Window matches season length to isolate trend [OK]
Hint: Match moving average window to season length to isolate trend [OK]
Common Mistakes:
  • Using too short window for moving average
  • Confusing differencing lag with season length
  • Ignoring trend when extracting seasonality