Transformations: log, power and binning
Reshape skewed variables so models see relationships more clearly, and learn what binning gives and costs.
Lesson 16 of 6612 minIntermediate
You will learn to
- Use log and power transforms on skewed data
- Know when binning helps or hurts
- Transform a target and map predictions back to the original scale
Before this lesson
Intuition
Many real quantities are not symmetric: incomes, prices, waiting times, counts. Most values are small and a few are huge. A model that assumes roughly symmetric relationships, such as linear regression, struggles with that: the few giants dominate the fit.
A transformation applies a function to every value to change the shape of the distribution. The most common one for right-skewed positive data is the logarithm. It turns multiplication into addition: the step from 10 to 100 and the step from 1,000 to 10,000 become the same size. Large values are compressed and small values are spread out, so the variable looks more symmetric and its relationship with the target is often closer to a straight line.
There are three tools to know:
- Log: simple, interpretable, for positive skewed data.
- Power transforms (Box-Cox, Yeo-Johnson): a family of transforms whose exponent is chosen from the data to make it as bell-shaped as possible.
- Binning: cut a numeric column into ranges ("under 30", "30 to 50", "over 50"). It trades detail for robustness and can capture non-linear effects with a simple model, but it throws information away.
Picture it
The math
Log. (written in code) works for , and keeps 0 at 0. Its inverse is .
Power transforms. Box-Cox, for strictly positive , with exponent :
Yeo-Johnson extends the same idea to zero and negative values. In both, is fitted from the training data by maximum likelihood, so it is a learned parameter, like the mean in standard scaling. When large values are compressed; when they are stretched.
Skewness measures asymmetry: 0 for a symmetric variable, positive for a long right tail.
Back-transforming a target. If the model predicts on the transformed scale, the prediction on the original scale is . For the log, . Because the transform is non-linear, the back-transformed value is closer to the median than the mean of the distribution of possible outcomes. For many uses that is fine; if you need the mean, apply a correction or model the original scale.
Worked example
Eight values of a skewed variable: 1, 2, 3, 5, 10, 20, 50, 200. (Computed with NumPy, SciPy and scikit-learn.)
- The mean is 36.4 and the median 7.5: the tail drags the mean far above the middle. The skewness is 2.04.
- After the values are 0.69, 1.10, 1.39, 1.79, 2.40, 3.04, 3.93, 5.30. The skewness falls to 0.68.
- A Yeo-Johnson transform fits on these eight values and brings the skewness down to about 0.17, closer to symmetric than the plain log.
Target example. Suppose the price of a house in thousands is 100 and 1,000 for two houses. Their log(1 + price) values are 4.62 and 6.91. A model that predicts 6.909 for a third house gives back exp(6.909) - 1, which is 1,000.0 (to the displayed precision). The mapping is automatic when the tool undoes the transform for you; if you do it by hand, always apply the inverse.
Binning. Age 18 to 80 cut into "under 30", "30 to 50", "over 50". A linear model can now give each band its own level, so it can capture an effect that rises, then flattens. The costs: everyone in a band is treated as identical (a person of 29 and one of 18 get the same value), the cut points are arbitrary, and a person of 30 and a person of 29 are suddenly "different". Prefer a power transform or a flexible model first; bin when the relationship is truly step-like, or for communication.
How mlflow does it
- Steps. In Pipeline Studio, the Change the shape of the values group of the Choose... list has Logarithm (log1p, for values above -1; no settings) and Make it bell-shaped (power transform), with method set to yeo-johnson (the default) or box-cox (strictly positive values only). Spread values evenly maps values to a uniform or normal distribution.
- Learned on training rows. The power transform's exponent is fitted on the training rows of each fold, like any other step.
- Recommendations. The recommender looks at skew and outliers to suggest scalers; transforms are yours to add.
- Quick train wizard. The Transformation toggle applies a Yeo-Johnson power transform to the numeric features.
- Time series targets. In a forecasting pipeline, scalers, Make it bell-shaped and Logarithm can be applied to the target series itself, and mlflow undoes the transform on the forecasts.
- Binning. There is no binning step at the moment. Prepare bands in your data before upload, using fixed, documented cut points, not ones computed from the test rows.
Common mistakes
- Using log on zero or negative values. The plain log is undefined there. Use log(1 + x) for non-negative data, or Yeo-Johnson for any values.
- Transforming before splitting. A fitted exponent uses all rows. Keep the transform in the pipeline.
- Forgetting to invert the target. Reporting errors on the log scale hides the real size of the mistakes. Always evaluate in the units people understand.
- Expecting trees to benefit. Tree models are unaffected by monotonic transforms of a feature, because they split on order, not on value.
- Binning by default. It discards information and creates artificial jumps. Use it for step-like effects or for explanation, and choose the cut points from domain knowledge.
- Judging by looks only. A transform that makes a histogram prettier may not help the model. Compare the cross-validated score with and without it.
Try it in mlflow
Add a power transform
- Open Pipelines and pick a regression pipeline with a skewed numeric column.
- In the Columns part, find the skewed column, press Choose... and, under Change the shape of the values, pick Make it bell-shaped. Compare Before and after in its chip.
- Choose Yeo-Johnson, which accepts zero and negative values, and train.
- Compare the run with a copy of the pipeline without the step.