Hermes Wiki
Developer/MachineLearning/FeatureEngineering/Fundamentals/feature-engineering-fundamentals-encoding-scaling-and-leakage

Feature Engineering Fundamentals: Encoding, Scaling, and Data Leakage

Concept

Feature engineering is the work of transforming raw data into the inputs a model actually consumes, and it routinely matters more to model quality than the choice of model architecture itself — a well-chosen feature set lets a simple model outperform a sophisticated one trained on poor features. Three concerns recur across almost every feature-engineering effort: encoding categorical data into a numeric form a model can use (one-hot encoding for nominal categories with no inherent order; ordinal/label encoding when a real order exists; target/embedding encoding for high-cardinality categories where one-hot would explode dimensionality), scaling numeric features onto comparable ranges so no single feature dominates purely because of its units (standardization to mean 0/standard deviation 1; normalization into a fixed range like [0, 1]), and data leakage — information reaching a feature that would not actually be available at prediction time, which inflates offline evaluation metrics while quietly destroying real-world performance.

As feature engineering scales beyond a single model or team, a feature store becomes the operational answer: a centralized repository that stores, versions, and serves curated features for both training and inference, so multiple models and teams reuse the same feature definitions instead of each reimplementing (and potentially miscomputing) the same transformation independently. Uber's Michelangelo platform popularized this pattern at scale with its Palette feature store — described internally as the single most important catalyst in scaling Uber's ML operations, precisely because feature reuse and a single source of truth for feature computation eliminated a whole class of training/serving inconsistency bugs.

Tradeoffs

Approach Benefit Cost
Manual, per-model feature computation No infrastructure investment, fast to prototype Feature-computation logic duplicated (and often subtly inconsistent) across models/teams; easy to compute a feature differently at training time vs. serving time
Centralized feature store Single source of truth, features reusable across models, built-in point-in-time correctness reduces leakage risk Real infrastructure and ownership cost; only pays off once enough models/teams share enough features to justify it
One-hot encoding Simple, no implied ordinal relationship, works with any linear/tree model Dimensionality explodes on high-cardinality categoricals (e.g. a "city" field with thousands of values)
Target/embedding encoding for high-cardinality categoricals Keeps dimensionality bounded regardless of category count Introduces its own leakage risk if the encoding is computed using the same rows it will be applied to, rather than out-of-fold
Standardization vs. normalization Standardization is robust to outliers affecting the overall scale less than normalization's fixed min/max range Normalization is more interpretable (bounded [0,1] range) but a single extreme outlier compresses the rest of the distribution into a narrow band

When to use / when not to

  • Reach for a feature store once more than one model or team is consuming overlapping features, or once training/serving consistency has already caused a production incident — building one preemptively for a single model is usually not worth the operational overhead.
  • Use one-hot encoding by default for low-cardinality nominal categories; switch to target or embedding encoding once cardinality gets high enough that one-hot's added columns meaningfully hurt training time or model size.
  • Always split data into train/validation/test by time (not randomly) when the target has any temporal structure — a random split lets future information leak into training rows through correlated near-duplicate records that a time-based split would have kept apart.
  • Fit scalers, encoders, and any other statistic-dependent transform on the training split only, then apply (not refit) those same fitted parameters to validation/test/serving data — refitting on the full dataset before splitting is one of the most common, and easiest to miss, sources of leakage.
  • Don't add a feature computed from information that would not exist at the actual moment of prediction in production (a "final order total" feature used to predict whether an order will be placed) — this is the leakage failure mode that inflates offline metrics the most severely and is the hardest to catch by inspecting the model itself, since the model performs correctly given a leaked feature; the bug is in what the feature is allowed to see.

Common pitfall

Fitting a scaler or encoder (computing the mean/std for standardization, or the category statistics for target encoding) on the entire dataset before splitting into train/validation/test. Because the fitted parameters were computed using rows that later end up in the validation or test split, the model gets an indirect, statistical preview of data it's supposed to be evaluated against as unseen — offline metrics look better than the model will actually perform once genuinely new data arrives in production. The fix is procedural: fit every such transform strictly on the training partition, then apply (never refit) the same fitted transform to validation, test, and eventual live inference data, so the exact same computation that will run at serving time is what got measured during evaluation.

Engineering Lens

Most feature-engineering failures that surface in production were invisible in offline evaluation, because leakage by definition makes offline metrics look better, not worse — a model with silently leaked features doesn't fail a validation check, it passes one that shouldn't have been possible to pass. The practical discipline this demands is treating "would this exact computation be available at the exact moment of a real prediction request" as a hard gate on every feature, not an afterthought caught by an accuracy metric. A feature store's point-in-time correctness guarantees exist specifically to make that gate structural rather than dependent on every engineer independently remembering to ask the leakage question for every feature they add.

Sources

Hermes Wiki