21 — Practice & Tooling

Feature Engineering#

Practice✓ Mathematical
◆ The PatternTurning raw timestamps into predictive signals

Time series features fall into three categories: lag features (past values), rolling statistics (windowed mean/std/min/max), and calendar features (day of week, month, holiday flags). Fourier features encode seasonality as continuous sine/cosine pairs.

// Interactive — feature construction
Feature TypeExamplesCaptures
Lag featuresy(t-1), y(t-7), y(t-30)Autoregressive patterns
Rolling statsrolling_mean(7), rolling_std(30)Trend, volatility
Calendarday_of_week, month, is_holidaySeasonal effects
Fouriersin(2πt/365), cos(2πt/365)Smooth cyclicality
# Python — time series features
df['lag_1'] = df['y'].shift(1)
df['lag_7'] = df['y'].shift(7)
df['roll_mean_7'] = df['y'].rolling(7).mean()
df['roll_std_30'] = df['y'].rolling(30).std()
df['dow'] = df.index.dayofweek
df['month'] = df.index.month
# Fourier features for yearly seasonality
df['sin_365'] = np.sin(2 * np.pi * df.index.dayofyear / 365)
df['cos_365'] = np.cos(2 * np.pi * df.index.dayofyear / 365)
Pattern bridge: Lag features are the tabular version of what AR models learn implicitly. Rolling statistics like rolling mean and rolling std are exactly moving averages and Bollinger Bands from technical analysis.
← Previous
N-BEATS & N-HiTS
Open in the full reader, with the topic sidebar →