Feature Construction
Invent signal the raw data only implies
Feature construction is the domain-informed creation of new columns — ratios, aggregates, lags, interactions, differences — that expose relationships models can't infer from raw fields.
What you will learn
- Build ratio, interaction, aggregate and lagged features from domain facts
- Test each constructed feature honestly against a baseline on validation
- Keep a feature manifest for production parity
What is this?
Feature construction proposes new columns that encode domain or temporal structure: per-unit ratios (amount per item), interactions (age x income groups), aggregates (customer lifetime total spend, rolling means), and temporal shifts (lag 1 demand, day-of-week). Each candidate must justify itself by improving validation score — the baseline is the model without it.
Why do you need it?
- Raw columns hide the relationships that matter: a $1M order is huge for a cafe, trivial for Amazon.
- Interactions express 'context matters': the sign of a feature may flip within another feature's group.
- Time-series models can't learn 'last week's demand' unless you hand it over as a lag feature.
- Good construction lets shallow (fast, interpretable) models match deep ones.
Mental model
A recipe that knows acidity balances sweetness
Domain-aware feature construction
The recipe didn't discover balance by staring at ingredients; a cook's knowledge proposed it. Same for features: know the business, propose the ratio, then the model confirms.
How it works
- Brainstorm from domain: ratios, margins, densities, per-unit economics.
- Interactions: multiply/cross numeric groups; for trees, combined columns can expose conditional splits.
- Aggregates: group by key (customer, product, region) -> count, mean, std, min, max.
- Temporal: lag_1, lag_7 rolling mean/std, difference from last period, holiday flags.
- Validate each candidate: holdout or CV score with vs without; drop silent features.
Feature construction sources.
Examples
Ratios, interactions and lags
import pandas as pdimport numpy as np df = pd.DataFrame({ "customer": ["a", "a", "b", "b", "c", "c"], "order_qty": [10, 20, 5, 15, 8, 12], "order_value": [100, 260, 30, 120, 60, 140], "date": pd.to_datetime([ "2026-01-01", "2026-01-15", "2026-01-02", "2026-01-16", "2026-01-03", "2026-01-17"]),}) df["price_per_unit"] = df["order_value"] / df["order_qty"]df["is_bulk"] = (df["order_qty"] >= 10).astype(int)df["qty_x_bulk"] = df["order_qty"] * df["is_bulk"] df = df.sort_values(["customer", "date"])df["lag1_value"] = df.groupby("customer")["order_value"].shift(1)df["rolling_value_mean"] = ( df.groupby("customer")["order_value"] .transform(lambda s: s.rolling(2, min_periods=1).mean())) print(df.round(2))price_per_unit normalizes across order sizes, the interaction flags bulk-with-value, and the lag/rolling features hand temporal memory to the model instead of making it rediscover last order.
In production
- E-commerce demand: lag-7 + rolling-28 demand plus promo flag cut MAPE by ~40% vs raw timestamps.
- Churn: 30/60/90-day spend velocity and tenure-based ratios beat raw monthly spend.
- Pricing: per-SKU margins and competitor-price ratios drive regression lift.
- Fraud: 1-hour transaction velocity and amount/vs-total ratio features are top ranked.
Best practices
- Source each feature from an audited business fact; derive test/prod features through one shared function so train and live get identical math; keep lag windows small (persistential state grows with window).
Common mistakes
- Building features from future rows (leakage in lag misuse); using validation rows in aggregates; overfitting to one leading edge feature that decays (holiday flags on old data); inventing features with no business rationale and hoping.
Debugging tips
- Plot each new feature vs target — visual signal check before submission.
- Watch for NaN from shift()/rolling on the first rows; decide fill vs drop deliberately.
- Compare feature importance ranks before/after construction to confirm new features earn weight.
Interview checkpoint
Quick quiz
Which of these risks target/date leakage?
A feature helps train but not validation. Likeliest cause?