Q&A-3How do you choose transformations for numerical features?
Answer
The 90-second answer. The estimator decides most of this. If the model splits on thresholds (gradient-boosted trees, random forests) any monotone transform (standardize, log, clip) produces an identical fitted model, because only the ordering of values matters. So for trees I transform only for what the model can't do itself: encode missingness, or make a feature's effect readable. For linear models, neural nets, and distance-based methods (kNN, SVM-RBF, k-means), scale is load-bearing, so I standardize or robust-scale. Then I look at shape: right-skewed positive quantities get log1p; heavy tails with genuine outliers get a robust scaler or a quantile transform. Every learned statistic is fitted inside the pipeline on training folds only and reproduced at serving.
Why scale is load-bearing. Put a feature ranging 0–1e6 next to one ranging 0–1 and the design matrix condition number is ~1e6; gradient descent needs roughly that many more steps to converge, and an L2 penalty of λ=1 shrinks the small-scale feature's coefficient far harder than the large-scale one. Standardizing makes the penalty uniform across features. Trees are immune because a split at x > 40 is the same split at log1p(x) > 3.71.
Worked example. session_duration_seconds: median 40, mean 120, p99 3000, max 90000. log1p gives 3.71, 8.01, and 11.41. The raw max/median ratio is 2250; after log1p it is 3.08, so the tail stops dominating the gradient. For a linear model I then standardize the logged column.
# scikit-learn 1.4
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer, StandardScaler
import numpy as np
log_then_scale = Pipeline([
("log", FunctionTransformer(np.log1p, feature_names_out="one-to-one")),
("scale", StandardScaler()),
])| Transform | Use when | Don't use when |
|---|---|---|
| Standardize | Linear/NN/distance; roughly symmetric | Trees; heavy outliers (mean/std get dragged) |
| RobustScaler (median/IQR) | Outliers present, linear model | You need bounded output |
log1p | Right-skewed, non-negative, multiplicative | Values can be negative; zeros are meaningful |
| QuantileTransformer | Arbitrary shape, want uniform/normal | Serving values fall outside training range |
| Clip at p1/p99 | Cap leverage of extreme points | The extremes are the signal |
Failure modes and detection.
- Leakage: fitting the scaler on all rows before splitting inflates validation. Detect by comparing fold-fitted vs global mean/std: a large gap means leakage.
log(0): yields-inf. Uselog1por clip at a floor; assert no-inf/NaN after the transform.- Serving drift: training median 40, serving median 400 shifts every standardized value. Monitor per-feature mean/std against training and alert on drift.
- Quantile clipping: values beyond the training range collapse to the [0,1] edges, losing resolution. Track the fraction of serving values sitting exactly on a boundary.
I keep an ablation against the raw baseline and judge on validation metric, calibration, and subgroup stability, not on how the histogram looks.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.