Shape of a Distribution showed a log pulling in a right skew; Detecting & Treating Outliers listed transforms as one way to tame an outlier. This chapter is the deep dive: the family of transforms, how to choose one, and the trap almost everyone hits when reporting the results.
x with a
nonlinear function f(x) (a log, a root, a power) to reshape the
distribution, reduce skew, stabilize variance, or straighten a relationship.
Standardizing (see Standardization & Z-Scores) is linear: it shifts and rescales but leaves the shape, skew, and kurtosis exactly as they were. Transformations here are nonlinear, so they genuinely change the shape. Don't confuse "scaling" with "transforming."
Why Transform
Reduce skew
Pull a long tail in so the variable is more symmetric, closer to the normality many methods assume.
Stabilize variance
When the spread fans out as values grow (heteroscedasticity), a transform flattens the fan.
Straighten a curve
A curved x-y relationship can become linear, which is exactly what linear regression wants.
Compress a range
Logs bring data spanning orders of magnitude (populations, prices) back onto a comparable scale.
On a log scale, equal distances mean equal ratios: a step is a percent change, not an absolute one. That is why pH, decibels, and the Richter scale are already logarithmic, the underlying quantity is multiplicative.
The Ladder of Powers
Tukey's ladder of powers is the one idea that organizes every transform. Order them by their exponent, with log sitting at the p = 0 rung. Going down the ladder pulls in a right skew; going up pulls in a left skew.
The Key Transforms
Each rung is a tool with its own job, requirements, and how you read it back.
| Transform | Good for | Requires | Notes |
|---|---|---|---|
| Log (ln / log₁₀) | Right skew, multiplicative data, wide ranges (income, prices, populations) | x > 0 (strictly positive) | Steps = percent change; back-transform with exp. For zeros use log1p. |
| Square root | Milder right skew; count / Poisson data (stabilizes variance) | x ≥ 0 | Variance ≈ mean for counts, so √ flattens it. |
| Reciprocal (1/x) | Very heavy right skew; rates (time → speed) | x ≠ 0 | Reverses the order of values, flag this. |
| Square (x²), cubes | Left skew (long left tail) | any sign | Going up the ladder. |
| Box-Cox | Auto-pick the best power to normalize | x > 0 | Chooses λ by maximum likelihood (below). |
| Yeo-Johnson | Same idea, but data has zeros / negatives | any real x | The Box-Cox generalization; sklearn's default. |
Box-Cox is the power family (xλ − 1) / λ (and ln x at λ = 0), with
λ chosen automatically by maximum likelihood to best normalize the data. The λ it picks
lands you on a familiar rung: λ≈1 no transform, λ≈0.5 square root,
λ≈0 log, λ≈−1 reciprocal. It needs positive data; when you have zeros or
negatives, reach for Yeo-Johnson instead.
Choosing, Interpreting & Pitfalls
The workflow is short: look at the distribution (a histogram and the skew, or a Q-Q plot), read the direction from the skew, try log or square root first because they are interpretable, and use Box-Cox or Yeo-Johnson when you just need normality for a model. Verify with the skew or a Q-Q plot afterward.
After transforming, every result lives on the new scale, you must back-transform to report
in real units. But the back-transformed mean is not the original mean:
exp(mean(log x)) gives the geometric mean, which sits below the
arithmetic mean for skewed data. So report the median (it back-transforms cleanly), or
apply a bias correction such as Duan's smearing estimator.
Positivity: log and Box-Cox need x > 0, use log1p, a justified shift, or Yeo-Johnson otherwise. The "+ constant" before a log changes the result, so document it. Reciprocal reverses order. Don't over-transform when the method is robust or interpretability matters (a log-dollar coefficient is harder to explain than dollars). And in ML, fit the transform (the λ, the shift) on training data only, the same no-leakage rule as scaling, imputation, and outlier thresholds.
Many classic models assume roughly normal, constant-variance, linear inputs, and a transform is often the cheapest way to meet those assumptions without abandoning the method. The cost is interpretability and the discipline of back-transforming honestly. Used well, a single log can turn a hopeless variable into a well-behaved one; used carelessly, it quietly biases every number you report.
Transformations in Machine Learning & AI
Transforming a skewed feature is one of the highest-value, lowest-effort moves in feature engineering. Many models simply work better when their inputs, and sometimes their target, are roughly symmetric.
| Transform | The scikit-learn tool | What it is for |
|---|---|---|
| Log / power | PowerTransformer (Box-Cox, Yeo-Johnson) | Pulls a skewed feature toward symmetry; Yeo-Johnson also handles zeros and negatives |
| Quantile | QuantileTransformer | Forces any distribution to uniform or normal, a heavy hammer for stubborn shapes |
| Target transform | TransformedTargetRegressor | Modeling log(price) instead of price often improves a regressor |
Like scaling and imputation, a power transform is learned (Box-Cox estimates a λ), so fit it on
the training set only. And when you transform the target, remember to invert the transform
on the predictions, a model trained on log(price) outputs logs, which you must exponentiate back
to dollars before anyone reads them.
Real-World Example: Response Times
API response times are reliably right-skewed, most requests are fast, a few are slow, and these 500 are no exception (skewness about 1.7). The companion notebook races four transforms: a log transform brings the skew almost to zero (about 0.1), a Box-Cox does slightly better, a square root only helps partway, and the winner turns a lopsided column into a near-symmetric one.
One row per request: request_id, endpoint (four API
routes), and response_ms, the server response time in milliseconds. The response column is strongly
right-skewed, the classic candidate for a log or Box-Cox transform.
Bring it to life in Python
The companion notebook logs a right-skewed variable straight, climbs the ladder of powers on one
variable, square-roots count data to stabilize its variance, lets Box-Cox pick the power with a Q-Q
check, and shows why exp(mean(log x)) is the geometric mean, not the arithmetic one.
View opens the rendered notebook instantly (no setup). Open in Colab runs &
edits it live in your browser. To run locally, install numpy, pandas,
scipy, matplotlib and launch jupyter notebook.
🎓 Key Takeaways
- ✓A transform is nonlinear: it reshapes the distribution (standardizing, from Standardization & Z-Scores, does not).
- ✓The ladder of powers: go down (√, log, 1/x) for right skew, up (x², x³) for left skew.
- ✓Log needs positive data and reads as percent change; square root stabilizes count variance.
- ✓Box-Cox auto-picks the power (λ); Yeo-Johnson handles zeros and negatives.
- ✓Back-transform to report, but exp(mean of logs) is the geometric mean (biased low), so report the median.
Practice Challenges
Five short challenges, beginner to intermediate. Try them on paper or in Python before checking the solutions.
Log the skew
Generate rng.lognormal(mean=2, sigma=0.9, size=3000), report its skew, log it, report the
skew again. Which way on the ladder did you move, and why does log need positive data?
scipy.stats.skew; log is a step down the ladder.Pick the direction
For each, say up or down the ladder and name a transform: (a) right-skewed income; (b) left-skewed exam scores; (c) Poisson counts to variance-stabilize.
Stabilize the variance
Make two Poisson groups (mean 5 and mean 100). Show the raw variances track the means, then square-root and show they become comparable.
Let Box-Cox choose
Apply scipy.stats.boxcox to a positive right-skewed sample. Report the chosen λ and what
values near 0, 0.5, and 1 would each imply.
Mind the back-transform
For a lognormal sample, compute the arithmetic mean and exp(mean(log x)). Are they equal?
Which should you report for a right-skewed quantity, and why?
A fully-worked solutions notebook walks through all five challenges in the same visual style. Try them yourself first, then compare.
Quiz: Test Yourself
Eight quick questions on transformations. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.