Apply Leakage-Safe Imputation and Ordinal Encoding
Quick Overview
Explain leakage-safe imputation and ordinal encoding for train and test data. Cover fit versus transform, business-defined category order, unseen levels, cross-validation, and stable output schemas.
Apply Leakage-Safe Imputation and Ordinal Encoding
Company: Capital One
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Online Assessment
You receive training and test tables with numeric columns, an ordered categorical column `service_tier` whose business order is `bronze < silver < gold`, and a binary training target. Some feature values are missing, and the test set may contain a tier not observed in training.
Explain how to produce `processed_train` and `processed_test` using imputation and ordinal encoding without leaking information from the test set. Address when to use `fit_transform` versus `transform`, how to preserve feature alignment, and how to handle unknown categories.
### Constraints & Assumptions
- The target is available only in the training table and must never enter a feature transformer.
- Numeric and categorical columns may use different imputation strategies.
- Both processed outputs must contain the same feature columns in the same order.
- Unknown or missing tiers must not be silently promoted to a higher business tier.
### Clarifying Questions to Ask
- Is the tier order fixed by the business or inferred from observed labels?
- Should missing tier values form a separate level or be imputed to a known level?
- Which estimator will consume the transformed features, and can it handle sentinel values?
- Is preprocessing evaluated inside cross-validation or only on one train/test split?
```hint Fit once on training data
Every learned statistic, including medians and category mappings, belongs to the training fit and is reused unchanged on validation and test data.
```
### What a Strong Answer Covers
- Separate numeric and categorical pipelines fitted only on training features.
- Explicit ordinal categories and a deliberate unknown-value encoding.
- Correct use of `fit_transform` on training data and `transform` elsewhere.
- Stable column names, ordering, dtypes, and row alignment.
- Cross-validation implications and serialization of the fitted pipeline.
### Follow-up Questions
- What leakage occurs if the imputer is fit before cross-validation folds are created?
- When would one-hot encoding be safer than ordinal encoding?
- How would you monitor a rising rate of unseen service tiers after deployment?
Quick Answer: Explain leakage-safe imputation and ordinal encoding for train and test data. Cover fit versus transform, business-defined category order, unseen levels, cross-validation, and stable output schemas.
You receive training and test tables with numeric columns, an ordered categorical column service_tier whose business order is bronze < silver < gold, and a binary training target. Some feature values are missing, and the test set may contain a tier not observed in training.
Explain how to produce processed_train and processed_test using imputation and ordinal encoding without leaking information from the test set. Address when to use fit_transform versus transform, how to preserve feature alignment, and how to handle unknown categories.
Constraints & Assumptions
The target is available only in the training table and must never enter a feature transformer.
Numeric and categorical columns may use different imputation strategies.
Both processed outputs must contain the same feature columns in the same order.
Unknown or missing tiers must not be silently promoted to a higher business tier.
Clarifying Questions to Ask Guidance
Is the tier order fixed by the business or inferred from observed labels?
Should missing tier values form a separate level or be imputed to a known level?
Which estimator will consume the transformed features, and can it handle sentinel values?
Is preprocessing evaluated inside cross-validation or only on one train/test split?
What a Strong Answer Covers Guidance
Separate numeric and categorical pipelines fitted only on training features.
Explicit ordinal categories and a deliberate unknown-value encoding.
Correct use of
fit_transform
on training data and
transform
elsewhere.
Stable column names, ordering, dtypes, and row alignment.
Cross-validation implications and serialization of the fitted pipeline.
Follow-up Questions Guidance
What leakage occurs if the imputer is fit before cross-validation folds are created?
When would one-hot encoding be safer than ordinal encoding?
How would you monitor a rising rate of unseen service tiers after deployment?