Classical machine learning
Scaling and transforms
Linear models, nearest neighbors and neural nets are sensitive to the size of numbers. Standardizing, log-transforming skewed values and using ratios puts features on comparable footing.
Many models care about the size of their inputs. A linear model treats a value of 10,000 very differently from a value of 10, and its weights shrink or grow to compensate. Scaling and transforming put numeric features on comparable footing before the model sees them. Trees are much less sensitive, because their split thresholds absorb the units.
Scaling
- Standardization computes
(x − μ) / σ. It is the default for linear and logistic models. - Min-max scaling maps values to
[0, 1]. It suits inputs with known bounds. - Robust scaling uses the median and the interquartile range, so outliers have less effect.
Fit the scaler on the training data only, then apply the fitted scaler to validation and test data. Fitting it on everything is a quiet form of leakage.
Log transforms for skewed values
Revenue, counts and bytes are often skewed, with a few very large values. log1p(x) = log(1 + x) compresses the tail while keeping zero valid, because log(0) is undefined.
Example
Request latency has a p99 of 2,400 ms and a median of 80 ms. On the raw scale, the p99 is 30 times the median, and a linear model is dominated by the spikes. After log1p, the values are log1p(2400) ≈ 7.78 and log1p(80) ≈ 4.39. The gap is now about 1.8 times, and the spikes no longer dominate. If you also add 0/1 flags, standardize the logged column so the scales are comparable.
Ratios
Raw counts depend on size. Raw error counts are not comparable between a large tenant and a small one, but errors / requests is. Decide in advance what a zero denominator means: missing, zero, or a separate flag.
Outliers: signal or noise?
Decide whether an extreme value is signal or noise before you act on it. A fraudulent $50,000 charge is a real event, and in fraud data it may be exactly the positive you want. A sensor reading of -999 is almost certainly noise. Winsorizing, which clips values at chosen percentiles, is a good default for glitches. Dropping outliers blindly is not.
Look before you transform
Plot histograms and count values before you choose a transform:
- Zero inflation. Many exact zeros can mean "none", or "missing logged as zero".
- Heavy tails. A few large values dominate sums. Prefer rates, logs or clipping.
- Strong label correlation. A category that is 99% positive is either a strong signal or leakage.
Common mistakes
- Scaling before the split. The validation data then shapes the scaler, and the score is optimistic.
- Taking
logof zero. Uselog1p. - Dropping every outlier. In fraud, abuse or medical data, the outliers may be the positives.
Check yourself
- Why use
log1pinstead oflogfor counts?log1phandles zeros, whilelog(0)is undefined. - Which common model type cares least about scaling? Trees, because their split thresholds absorb the units.
Key takeaways
- Standardize for linear and logistic models, and fit the scaler on training data only.
- Use
log1pfor skewed positive values, and check the tail before choosing a transform. - Prefer ratios when the size of the group varies, and define the zero-denominator case.
- Decide whether each outlier is signal or noise before you remove it.
Further learning
Numeric features are only half the story. The next page covers categories and text: encoding categories and text.
Related
Features and data preparation
Turning raw data into model inputs without leaking the future. Covers scaling, encoding, missing values, time windows, target encoding and class imbalance, and keeping training and serving identical.
Linear and logistic regression
The two models under almost everything in ML. They fit lines, predict probabilities and stay honest through regularization.
Data leakage
Leakage is when a model learns from information it would not have at prediction time. Offline scores look excellent, then production falls apart. Here is how to find it.