0
0
ML Pythonml~20 mins

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

Choose your learning style9 modes available
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.