When scaling matters
Linear regularization, k-nearest neighbors, k-means, and neural-network optimization react to numeric magnitude. A salary measured in dollars can dominate a visit count simply because its numbers are larger. Tree-based models usually do not need scaling because their splits depend on ordering rather than distance.
Pick a scaler from the distribution
Use StandardScaler for reasonably symmetric values, RobustScaler when outliers would distort the mean and standard deviation, and MinMaxScaler when bounded inputs are required. Long-tailed amounts often benefit from a log transform before scaling. Remember that future values can exceed the training min and max.
Encoding categoricals
- One-hot for low-cardinality unordered categories, compatible with most models.
- Ordinal only when order is real; arbitrary integer codes invent a false distance.
- Target encoding for high cardinality, always estimated inside training folds to prevent leakage.
- Hashing when a stored vocabulary is impractical and collisions are acceptable.
Plan for unseen categories
Production will contain a city, product, or code absent from training. Decide whether the encoder errors, maps it to an explicit unknown bucket, or hashes it. Monitor the unknown rate because silently collapsing every new category can hide meaningful drift.
Pipeline placement
Scalers and encoders belong inside a ColumnTransformer and model pipeline. Fit them on training only and apply the same fitted objects to validation and production. Select columns by stable names rather than position, then inspect the transformed names and output shape.
Test the serialized path
Load the saved pipeline in a fresh process and score raw rows with reordered columns, missing optional values, and unseen categories. If a notebook cell or hand-maintained feature order is still required, training and serving do not yet share one contract.