Feature Engineering: lesson 2 of 4
Feature Engineering
Interaction, Ratio, and Aggregation Features
Combine existing columns into representations that expose useful predictive relationships.
Concept
Raw columns are not always the most useful representation. Interaction, ratio, and aggregation features combine available information into signals that better describe an entity or decision.
Interactions and Ratios
Sometimes a feature matters differently alongside another feature. Debt and income are individually useful, but debt_to_income = debt / income often describes financial burden more directly. Similarly, sessions relative to account age can be more informative than sessions alone.
customers["debt_to_income"] = customers["debt"] / customers["income"]
customers["sessions_per_month"] = customers["sessions"] / customers["tenure_months"]
Ratios need meaningful denominators. Guard against zero, tiny denominators that create unstable values, and fields unavailable at prediction time. A ratio that sounds useful can still be leaky: price_per_area is invalid when price is the target.
Aggregations Describe Entities
Transaction tables often have many rows per customer, while a churn model predicts one customer. Aggregation can turn events into customer features:
customer_summary = orders.groupby("customer_id").agg(
total_spend=("revenue", "sum"),
order_count=("order_id", "count"),
average_order_value=("revenue", "mean"),
maximum_purchase=("revenue", "max"),
)
These features change the row grain from event to customer. They are useful only when the aggregation window ends at the prediction date. Predicting churn on March 1 with purchases from March 10 is temporal leakage, even if the groupby code is correct.
Failure Signals
Common Mistakes
- Dividing by zero or unstable denominators.
- Aggregating future events.
- Mixing customer-level and event-level rows without checking grain.
- Treating every combination as useful.
Interview Perspective
Question: How can aggregation create leakage? Answer: an aggregate includes events occurring after the prediction time. What the interviewer is testing: whether feature design respects time and entity grain.
Practice Questions
- Design a useful ratio for credit utilization.
- Why can a tiny denominator be dangerous?
- Create two customer-level features from orders.
- Which purchases may a March 1 churn feature include?
- Why is price per area invalid when price is y?
Key Takeaway
Key Takeaways
Interactions, ratios, and aggregates can expose useful relationships, but must respect denominator meaning, entity grain, and prediction time.
Next Lesson
Next, derive signals from time and domain knowledge.
Finish this lesson on your terms
Mark it complete when you have worked through the material and are ready to move on.