Contents/ Part IV · Preparing Data for Analysis/ Chapter 22

Transformations

Sometimes the data is fine but its shape fights you: a long tail, a fan of growing variance, a curve where you wanted a line. A transformation reshapes the variable to cooperate, and you only have to remember to change it back.

⏱️ ~13 min read
🐍 Notebook included
📊 Chapter 22

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.

f(x)
A transformation replaces each value 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.
🔁
Nonlinear, unlike standardizing

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."

1

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.

2

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.

x³ , x² (p>1) x (p=1, none) √x (p=0.5) log x (p=0) 1/x (p=−1) UP pulls in a LEFT skew DOWN pulls in a RIGHT skew
Right-skewed data (income, counts) goes down the ladder; left-skewed data (ceiling effects) goes up. The lower you go, the stronger the pull.
3

The Key Transforms

Each rung is a tool with its own job, requirements, and how you read it back.

TransformGood forRequiresNotes
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 rootMilder right skew; count / Poisson data (stabilizes variance)x ≥ 0Variance ≈ mean for counts, so √ flattens it.
Reciprocal (1/x)Very heavy right skew; rates (time → speed)x ≠ 0Reverses the order of values, flag this.
Square (x²), cubesLeft skew (long left tail)any signGoing up the ladder.
Box-CoxAuto-pick the best power to normalizex > 0Chooses λ by maximum likelihood (below).
Yeo-JohnsonSame idea, but data has zeros / negativesany real xThe Box-Cox generalization; sklearn's default.
🎚️
Box-Cox: a slider across the ladder

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.

4

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.

↩️
The back-transform trap (Jensen's inequality)

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.

⚠️
The usual traps

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.

🤖
Why this matters for data science

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.

5

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.

TransformThe scikit-learn toolWhat it is for
Log / powerPowerTransformer (Box-Cox, Yeo-Johnson)Pulls a skewed feature toward symmetry; Yeo-Johnson also handles zeros and negatives
QuantileQuantileTransformerForces any distribution to uniform or normal, a heavy hammer for stubborn shapes
Target transformTransformedTargetRegressorModeling log(price) instead of price often improves a regressor
🤖
Fit the transform on training data, and remember to invert it

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.

6

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.

📂 Dataset · transformations--response_times.xlsx

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 Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub

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.
7

Practice Challenges

Five short challenges, beginner to intermediate. Try them on paper or in Python before checking the solutions.

1

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?

Hint: scipy.stats.skew; log is a step down the ladder.
2

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.

Hint: right → down; left → up; counts → square root.
3

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.

Hint: Poisson variance ≈ mean; √ pulls both toward ~0.25.
4

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.

Hint: λ≈0 log, λ≈0.5 sqrt, λ≈1 none.
5

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?

Hint: exp(mean log) is the geometric mean (Jensen); prefer the median.
Check your work

A fully-worked solutions notebook walks through all five challenges in the same visual style. Try them yourself first, then compare.

📓 View Solutions ▶ Open Solutions in Colab ⬇ View / Download on GitHub
8

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.