Feature Engineering: lesson 3 of 4
Feature Engineering
Time-Based and Domain-Driven Features
Turn dates, timestamps, and domain knowledge into useful predictive signals.
Concept
Dates and timestamps contain structure that raw strings hide. Domain knowledge helps decide which parts of that structure represent useful, prediction-time signals.
Date-Derived Features
events["timestamp"] = pd.to_datetime(events["timestamp"])
events["hour"] = events["timestamp"].dt.hour
events["day_of_week"] = events["timestamp"].dt.dayofweek
events["month"] = events["timestamp"].dt.month
events["is_weekend"] = events["day_of_week"].isin([5, 6]).astype(int)
Signup dates can become account age; last-purchase dates can become days since last purchase. For e-commerce, repeat-purchase rate, recency, and basket size can represent customer behavior. Finance may use debt-to-income or utilization; housing may use property age or rooms per area.
Time Is Cyclical
Hour 23 and hour 0 are adjacent in reality but far apart as raw numbers. Sine/cosine encodings can represent this cycle when time-of-day patterns matter. The goal is not trigonometry; it is avoiding a representation that treats midnight as distant from 11 PM.
Validate Domain Intuition
Domain-inspired does not mean automatically valuable. days_until_churn is invalid for churn prediction because it knows the outcome. Compare a feature using the same validation setup as the baseline and keep it only when it improves useful held-out behavior.
Practice Questions
- Create two features from a signup date.
- Why can hour require cyclic representation?
- Is days until churn valid for churn prediction?
- Name an e-commerce recency feature.
- Why must domain intuition be validated?
Key Takeaway
Key Takeaways
Time and domain features can reveal behavior, but their meaning, timing, and validation matter more than clever names.
Next Lesson
Next, test whether engineered features are valid, stable, and worth maintaining.
Finish this lesson on your terms
Mark it complete when you have worked through the material and are ready to move on.