Feature Engineering & Data Leakage
ML Engineer path · lesson 70 of 80Amazon had over two million brands in 2019, and three obvious encodings each fail one step later. Watch the hashing trick absorb an unbounded category space, then toggle scale-before-split to see a statistic carry the test set into training.
Key insight
“There is no perfect way to handle missing values.” Deletion risks losing information and accentuating bias; imputation risks injecting bias, adding noise, or data leakage. The choice is between two known costs, so the useful question is which cost this particular column can afford.
Leak or safe?
Sort each practice by whether it leaks information from the test set into training.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
Leaks
Drop items hereSafe
Drop items hereTest your knowledge
Income missing because high earners decline to disclose it is an example of:
The short version
A recipe where the ingredients matter more than the technique. And where one of the ingredients is a taste of the finished dish, which makes every practice run go beautifully and tells you nothing about dinner.
In more depth
Feature Engineering & Data Leakage
"Practical Lessons from Predicting Clicks on Ads at Facebook" (He et al., 2014) claimed having the right features is the most important thing — once you have a workable model, the right features give a bigger boost than clever algorithmic techniques such as hyperparameter tuning.
Missing values — three kinds
| Kind | Meaning | The trap |
|---|---|---|
| MNAR | Missing because of the true value itself | Removing rows removes information — the fact that income is missing is itself a signal |
| MAR | Missing because of another observed variable | Removing rows creates bias — drop missing-age and you drop every gender-A respondent |
| MCAR | No pattern | "This type of missing is very rare." |
Row deletion works for MCAR when the affected count is small, such as under 0.1%. Avoid filling missing values with possible values — filling number-of-children with 0 makes missing indistinguishable from childless. Cautionary bug: a frontend stopped asking for age, the pipeline filled it with 0, and the model never saw 0 during training.
"There is no perfect way to handle missing values."
Scaling
x' = (x − min)/(max − min) · to [a,b] · x' = (x − x̄)/σ. "Empirically, I find the range [−1, 1] to work better than [0, 1]." Log transformation for skew.
Two notes that matter later: scaling is a common source of data leakage, and it requires global statistics — so at inference you reuse the training statistics, which means you must retrain often.
Discretization: "In practice, I've rarely found discretization to help." The boundary problem — $34,999 is now completely different from $35,000, which is treated the same as $100,000.
The hashing trick
Amazon had over two million brands in 2019. Three encodings, each failing one step later: integer indices crash; an UNKNOWN bucket means the model never saw UNKNOWN; top-99%-plus-UNKNOWN means 20 new brands in an hour all get treated like unpopular brands.
The hashing trick (popularised by Vowpal Wabbit) fixes the number of encoded values in advance without knowing the category count. An 18-bit space gives 2¹⁸ = 262,144 indices. Collisions are random, so a new brand can collide with any brand — and Booking.com found that even at a 50% collision rate, log-loss performance loss is under 0.5%.
Data leakage
"Data leakage refers to the phenomenon when a form of the label 'leaks' into the set of features used for making predictions, and this same information is not available during inference."
Six causes: random splits of time-correlated data · scaling before splitting · filling missing data with test statistics · duplication before splitting (3.3% of CIFAR-10 and 10% of CIFAR-100 test images have duplicates in training) · group leakage · leakage from the data generation process (the hospital that routed suspected cancer patients to a better scanner).
Five detections: per-feature predictive power (two features can be clean individually and leak together — start date and end date give away tenure) · ablation studies · suspicion of any new feature that helps a lot · care with the test split · subject matter experts.
Good features
Five costs of too many: leakage · overfitting · memory · latency · technical debt.
Facebook's ads team: the top 10 features are responsible for about half the total feature importance, while the last 300 contribute less than 1%.
Generalization: coverage (and the exception — a feature in 1% of data where 99% are POSITIVE is useful) and distribution overlap (DAY_OF_THE_WEEK with train Mon–Sat and test Sun will not generalise).
Interview Questions
References & Further Reading
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required