Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Giving the model the right clues
Telling someone “it’s 23 o’clock” and “it’s 0 o’clock” makes the two times sound far apart, yet they are minutes apart. Calling sensors “1, 2, 3, …” suggests sensor 3 is “more” than sensor 2, which is meaningless. Feature engineering means rewriting raw data so the clues a model gets match what is actually true about the world.
Engineering data are full of angles, times of day, sensor IDs, unit conversions and missing readings. Encoding them sensibly (a heading as sin and cos, an orbit phase on a circle, a sensor ID by its bias) can turn a useless linear model into an accurate one without changing the algorithm.
- 1. Why is 'hour = 23' versus 'hour = 0' a bad raw feature?
- 2. Numbering categories 1, 2, 3 suggests…
- 3. In the demo, which encoding fits the daily cycle with 2 extra columns?
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.The standard transformations
| Data | Transformation | Notes |
|---|---|---|
| numeric, different scales | standardise (z-score), min–max, robust (median/IQR) | needed for distance, gradient and regularised models; not for trees |
| skewed positive values | log(1 + x), Box–Cox, Yeo–Johnson | makes effects closer to linear |
| periodic (hour, angle, day of year) | sin and cos of 2πx/period | no jump at the wrap |
| nominal categories (few) | one-hot | unknown categories → all zeros |
| nominal categories (many) | target or frequency encoding, hashing, embeddings | target encoding needs out-of-fold fitting |
| ordinal categories | ordered integers | only if the order is real |
| missing values | median/mean/mode imputation + missing indicator, kNN or iterative imputation | fit on training data |
| continuous → coarse | quantile or uniform binning | captures non-linearity for linear models |
| combined effects | products xᵢxⱼ, ratios, domain formulas | e.g. power = voltage × current |
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder, TargetEncoder, FunctionTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import make_pipeline
pre = ColumnTransformer([("num", make_pipeline(SimpleImputer(strategy="median", add_indicator=True), StandardScaler()), num_cols),
("cat", OneHotEncoder(handle_unknown="ignore"), small_cat_cols),
("ids", TargetEncoder(), big_cat_cols)]) # cross-fitted internally- 1. Which models do NOT need feature scaling?
- 2. A heading angle in degrees should be encoded as…
- 3. Unknown categories at prediction time with one-hot encoding become…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.Fit on training data only
Every transformer has a fit step that learns statistics (means, categories, medians, bin edges, category means) and a transform step that applies them. Fitting on all data leaks information (D0.3).
Target encoding replaces a category by its (smoothed) mean target:
(n_c·ȳ_c + m·ȳ) / (n_c + m).
Smoothing pulls rare categories toward the global mean. Encoding a training row with a mean that includes its own label leaks the answer, so training rows are encoded out of fold. Ordinal and frequency codes carry no information about per-category effects unless those effects happen to follow the code. One-hot encoding with regularisation is the safe baseline for a linear model.
- 1. Smoothed target encoding of a category with 0 examples equals…
- 2. Why encode training rows out of fold?
- 3. Robust scaling divides by…
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.Good habits
- Use domain knowledge:
- physics features: dynamic pressure ½ρv², Reynolds number, power = V·I;
- ratios and differences rather than raw pairs;
- lag features for time series.
- Match encodings to models. Trees handle raw scales and ordinal codes reasonably, but suffer with one-hot of huge cardinality. Linear and distance models need scaling, one-hot or target encoding, and explicit non-linear features.
- Keep everything in a pipeline so the exact same steps run in training, validation and production.
- Check feature value: ablation, permutation importance (D16), learning curves.
- Beware of combinatorial blow-up: p features give p(p−1)/2 interactions. Regularise or select.
Interview questions.
- How do you encode a categorical with 10 000 levels? Out-of-fold target encoding, hashing, or learned embeddings; group rare levels.
- Why add a missing indicator after imputation? Missingness can carry information (D0.2), and imputation alone hides it.
- 1. 10 000-level categorical for a linear model:
- 2. Number of pairwise interactions of 20 features?
- 3. Why use a pipeline for feature engineering?
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Which encoding? A measured comparison
Exercise 1.
- Robust scaling centres on the median (2.5), ignoring the outlier at 100.
- Smoothed target encoding gives category A (3 of 4 positive) the value 0.667 with m = 2.
- Out-of-fold values differ from in-sample values, as they must.
- Cyclic encoding places 23:59 within 0.01 of midnight.
Exercise 2: encoding a sensor ID and the hour of day. Setup:
- 150 sensors, each with its own bias (SD 1.5), most seen 2–25 times;
- a daily cycle of amplitude 1.5, a temperature effect, and noise σ = 1;
- ridge regression on 1 500 training readings, test RMSE on 5 000 new readings from the same sensors, 10 repetitions.
| Sensor encoding | Train RMSE | Test RMSE |
|---|---|---|
| none | 1.765 | 1.768 |
| ordinal ID | 1.758 | 1.762 |
| frequency | 1.764 | 1.766 |
| one-hot (150 columns) | 0.970 | 1.049 |
| target, in-sample | 1.036 | 1.119 |
| target, out-of-fold | 1.151 | 1.115 |
| Hour encoding (with one-hot sensors) | Test RMSE |
|---|---|
| raw hour | 1.388 |
| one-hot hour (24 columns) | 1.058 |
| sin/cos (2 columns) | 1.049 |
- Ordinal and frequency codes are useless here. The sensor’s number and popularity say nothing about its bias.
- One-hot with ridge captures the per-sensor bias and gets close to the noise floor of 1.0.
- A single target-encoded column gets most of the way (1.12).
- In-sample target encoding looks 7% better on the training data than it really is. The out-of-fold version’s training error is honest. With this linear model, the leak mainly misleads evaluation; with flexible models it also hurts the fit.
- For time: a straight line in raw hours misses the cycle. Two sin/cos columns do as well as 24 one-hot columns.
Feature engineering in AI, robotics and aerospace
- Flight data: angles and headings as sin/cos; dynamic pressure, Mach number and other dimensionless groups as features.
- Telemetry: rolling statistics, lags and rates of change; per-unit target encoding of serial numbers with care for leakage.
- Orbital data: orbital phase and local solar time as cyclic features.
- Robotics: relative poses (differences of positions in a body frame) rather than absolute coordinates.
References
- M. Kuhn, K. Johnson (2019). Feature Engineering and Selection: A Practical Approach for Predictive Models. CRC Press.
- D. Micci-Barreca (2001). “A preprocessing scheme for high-cardinality categorical attributes in classification and prediction problems.” ACM SIGKDD Explorations 3(1).
- A. Zheng, A. Casari (2018). Feature Engineering for Machine Learning. O’Reilly.
- K. Weinberger et al. (2009). “Feature hashing for large scale multitask learning.” ICML.
- scikit-learn developers. Preprocessing data and TargetEncoder (cross fitting), user guide.