Overview
Curated: · Written: · Reviewed:
Feature Engineering & Selection
In one line
Turning raw data into model-ready inputs, keeping only features whose value was knowable at the prediction moment and that improve held-out performance. Reach for it whenever model inputs come from event logs, tables or request payloads.
Key definitions
- Point-in-time correctness: each value uses only rows with event_time and ingestion_time at or before the prediction time.
- Target leakage: a feature that carries the outcome, or data written after it.
- Training-serving skew: the same feature computed differently offline and online.
- Out-of-fold encoding: target statistics computed only from folds other than the row's own.
- Missingness indicator: a binary column recording that a value was absent, kept beside the imputed value.
- Permutation importance: the held-out score drop when one column is shuffled. Correlated substitutes hide each other.
Formulas and complexity
Smoothed target encoding:
Standardization uses training-fold statistics only:
Cyclic hour encoding : the Euclidean distance between 23:00 and 00:00 is , and between 23:00 and 20:00 it is . Centering a sparse one-hot matrix fills every zero. For 200,000 rows by 5,000 float64 columns that is .
How it works
- Define the entity, prediction time, label horizon and action. Record each feature's event time, ingestion time, source, owner and serving path.
- For each prediction, take the row with the latest event_time among rows whose ingestion_time is at or before the prediction time.
- Fit every learned step (imputer, scaler, vocabulary, target encoder, selector) inside the training fold, then apply it to validation or test rows.
- Run the same fixtures through offline and online transform code and compare values, types, unknown categories and rounding.
Worked join from the feature store table: P1 at 2024-03-06 gets 90 (event 03-04, ingested 03-04). The 200 row was ingested 03-09, so it is invisible. P2 at 2024-03-10 gets 150 (event 03-07, ingested 03-08). The 200 row has an earlier event_time, so the latest event wins.
When to use and when not to
| Choice | Use when | Avoid when |
|---|---|---|
| Standardize | Distance, gradient or penalty models (k-NN, SVM, regularized linear) | Tree splits, where scaling changes nothing |
| Impute plus missing flag | Absence carries signal, e.g. lactate drawn only when infection is suspected | Absence has no mechanism and is pure noise |
| One-hot | Low or moderate cardinality, unordered categories | Thousands of categories; use hashing or out-of-fold target encoding |
| Filter selection | One pick reused across several model families, fast enough for the budget | Model-specific interactions matter and the budget allows wrappers |
| Wrapper selection | Small candidate sets with budget for repeated fits | Hundreds of features under a tight refresh deadline |
| Feature cross | Domain-motivated pairs with dense data | Rare combinations, which overfit |
Traps interviewers probe
- Fitting the imputer, scaler, encoder or selector on all rows before splitting. Fix: put them in a Pipeline and run it inside each CV fold. For time data, use TimeSeriesSplit.
- Imputing away a signal. Fix: a NULL refund that means no refund becomes 0.00. A NULL sensor reading that means failure stays missing, with an indicator.
- Dropping a rare but strong binary feature with VarianceThreshold. A column with 4 fraud rows out of 1,000 has variance 0.004 × 0.996 ≈ 0.004, below 0.01, so it is removed. Fix: select on a score that reflects the label, and check the rows that were dropped.
- Taking an unseen category as harmless. With drop='first' and handle_unknown='ignore', an unseen country encodes as all zeros, which equals the dropped reference. Fix: state that behavior, count unknown rates, and monitor them.
- Logging amounts with zeros or negatives. log(0) is -inf, and log(-12.5) is nan. Adding 1e-9 turns refunds into about -20.7 and leaves chargebacks as nan. Fix: use a sign-aware transform or a separate flag.
Interview tips
Open with: a feature is valid only if its value was knowable at the decision, and every learned step is fit inside the training fold.
Expected follow-ups:
- Late-arriving data: use ingestion_time, not event_time, to decide availability.
- Skew: replay the same raw requests through both paths and diff the feature values.
- Importance versus causality: importance is predictive only. Answer a causal question with an experiment.
- Retirement: remove consumers first, then the writer, then the store entry.
30-second outline:
- Name the entity and prediction time.
- State the point-in-time join rule.
- Fit all learned steps inside a pipeline per fold.
- Handle missingness by mechanism, with an indicator where absence carries signal.
- Run selection inside CV, and use grouped permutation or ablation for correlated features.
- Describe the offline/online parity test and the monitoring of feature distributions.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.