M2-065M2: Classical Machine LearningFeature EngineeringMedium
⚡Flashcard Drill➔Mastery:

Key Points in Time Feature Engineering and Preventing Temporal Leakage

🎯 Active Recall Self-Check (Draft First, Verify After)
Login-free practice tracker: recall core answer, audit pitfalls, and log review status.
Step 1 · Draft your concise answer & assumptions
Step 2 · Check Pitfalls & Follow-up Inquiries
Next: M2-066 →
📐 Mathematical Definition
rolling mean: xˉt−1−w:t−1 (不含 t)\text{rolling mean}:\ \bar x_{t-1-w:t-1}\ (\text{不含}\ t)
⚡ One-Sentence Core Answer
Construct lag features, rolling window statistics, cyclical encodings, and elapsed durations while strictly ensuring right-aligned windows to avoid future data leakage.

📌 Key Takeaways

  • •
    Cyclical transformation: encode hour/day/month via sine and cosine to preserve periodicity
  • •
    Rolling window statistics: ensure windows are right-aligned using strictly historical data
  • •
    Temporal validation: use TimeSeriesSplit or forward-chaining, never random shuffle CV

📐 Mathematical Derivations

Core temporal feature categories: ① Cyclical Encoding: For periodic features (e.g., hour h∈[0,23]h \in [0, 23] or weekday w∈[0,6]w \in [0, 6]), avoid linear integer encodings where 23 and 0 appear maximally distant. Map to 2D coordinates: xsin⁡=sin⁡(2πtT)x_{\sin} = \sin\left(\frac{2\pi t}{T}\right), xcos⁡=cos⁡(2πtT)x_{\cos} = \cos\left(\frac{2\pi t}{T}\right), preserving circular Euclidean distance. ② Lag Features: xt−kx_{t - k} capturing autoregressive dependencies. ③ Rolling Aggregations: μt,W=1W∑i=1Wxt−i\mu_{t, W} = \frac{1}{W} \sum_{i=1}^W x_{t-i}, strictly excluding xtx_t if predicting target at time tt.

🏭 Production Trade-offs

Production safeguards against leakage: ① Right-Aligned Windows: Rolling statistics must shift backward by at least the inference prediction horizon (e.g., if predicting 24 hours ahead, lags must start at t−24t-24). ② Feature Arrival Delays: Account for logging latency in production pipelines (features might only become available hours after timestamp). ③ Cross-Validation Architecture: Strictly apply forward-chaining (expanding window or rolling window splits); never use standard randomized K-fold.
⚠️ Common Interview Pitfalls
  • ✕
    Using future information in rolling window aggregations (e.g., centering the window at timestamp tt)
  • ✕
    Evaluating time-series models using random K-fold cross-validation, leading to overly optimistic benchmark scores
🎯 Interviewer Follow-up Inquiries
  • ?
    Why is sine/cosine encoding necessary for hour-of-day features in distance-based and linear models?
  • ?
    How do you handle production logging delays when designing real-time lag features?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

← PreviousM2-064: Feature Interactions and Polynomial Features: Concepts and Scenarios📋 Back to BankNext →M2-066: What is the Hashing Trick (Feature Hashing)? Pros and Cons