Walk me through feature engineering for tabular data
Written by the mlprep team · Updated
Walk me through your feature engineering process for a new tabular ML problem. What do you look at first, and what transformations do you commonly apply?
Start with data understanding#
Before any transformation: know your data types (numeric continuous, numeric discrete, ordinal, nominal, datetime, text, boolean), cardinalities, null rates, and distributions. Feature engineering decisions cascade from these.
Handling missing values#
Never drop columns with missing data reflexively — missing itself is often informative.
For numeric features:
- Impute with mean (Gaussian-ish distributions) or median (skewed / outlier-prone)
- Add a binary
feature_is_missingindicator alongside the imputed value — preserves the signal that data was absent - For tree models: some implementations handle NaN natively; explicit indicators rarely help
For categorical features:
- Treat missing as its own category ("UNKNOWN") — simple, effective, preserves the pattern
- Impute with mode if you have evidence it's truly random missingness
Numeric transformations#
Log transform: for right-skewed features (income, price, count data). log(x+1) handles zeros. Makes distributions more Gaussian, helps linear models and distance-based methods.
Clipping: cap outliers at the 1st/99th percentile to prevent a handful of extreme values from dominating gradients or distance calculations.
Binning: convert continuous to ordinal buckets. Useful for noisy numeric features where the exact value doesn't matter (age → age_group). Lets tree models find non-linear cuts more easily.
Interactions: multiply or ratio two features where their combination has meaning — price_per_sqft = price / sqft, clicks_per_impression = clicks / impressions.
Scaling (for non-tree models)#
Standard scaling: z = (x - μ) / σ. Centers at 0, unit variance. Required for logistic regression, SVMs, neural nets (otherwise features with larger ranges dominate).
Min-max scaling: z = (x - min) / (max - min). Preserves zero, outputs [0, 1]. Sensitive to outliers.
Robust scaling: uses median and IQR instead of mean and std. Better when outliers are present.
Critical: fit scalers on training data only. Apply the same transformation (using training statistics) to val and test. Fitting on all data = data leakage.
Tree-based models (RF, GBM) are invariant to monotonic feature scaling — no need to scale.
Categorical encoding#
See categorical-encoding question for depth. Short version:
- Low cardinality (under ~20 values): one-hot encoding
- High cardinality: target encoding or embedding
- Ordinal: integer encoding preserving order
Datetime features#
Extract: hour of day, day of week, month, quarter, is_weekend, is_holiday, days_since_event. Cyclical encoding (sin/cos) for hour and day-of-week so that 23:00 and 00:00 are adjacent in feature space.
df['hour_sin'] = np.sin(2 * np.pi * df['hour'] / 24)
df['hour_cos'] = np.cos(2 * np.pi * df['hour'] / 24)The pipeline matters#
All transformations that use training statistics (scaling, mean imputation, target encoding) must be inside a proper train/val/test split. Use sklearn's Pipeline + ColumnTransformer to prevent leakage and ensure consistent application at inference.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Feature engineering pipeline: (1) understand data types and distributions, (2) handle missing values — impute + add indicator, (3) transform numerics — log for skew, clip outliers, create ratios, (4) scale for non-tree models using training stats only, (5) encode categoricals by cardinality, (6) decompose datetimes into cyclical features. Always fit on training data; apply to val/test. Tree models skip scaling; linear models and NNs need it.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How do you prevent data leakage when using target encoding in cross-validation?
- Your dataset has a feature with 10% missing values. The missingness correlates with the target. How do you handle it?
- When would you use automated feature engineering (e.g., Featuretools) vs. manual domain-driven engineering?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
How do you encode categorical features? Compare one-hot, target encoding, and embeddings.
When do feature crosses matter, and how do you control their complexity?
How do you handle missing data without introducing bias?
How would you design a feature store for a large ML platform?