2.1.9 · D5Data Preprocessing & Feature Engineering
Question bank — Log and power transformations
Before we start, some plain-word reminders so every symbol below is earned:
True or false — justify
A power transform changes the ranking of your data points.
False. Log, sqrt, Box-Cox and Yeo-Johnson are all strictly increasing (monotonic) on their domains, so the largest value stays largest — only the spacing between points changes.
Applying to a right-skewed feature always makes it perfectly Normal.
False. Log reduces right skew and often helps, but "reduce" is not "eliminate"; the result can still be skewed, bimodal, or over-corrected into left-skew. Normality is a goal, not a guarantee.
and give effectively the same result for large .
True. When , the is negligible: . The offset only matters near zero, where it rescues from being undefined.
Box-Cox with leaves the shape, spread, and ranks of the data unchanged.
True. is a pure shift by — nothing about the distribution's form or ordering moves, so a fitted signals "no transform needed."
Yeo-Johnson can always replace Box-Cox with no downside.
False. Yeo-Johnson handles zeros and negatives, but on strictly-positive data Box-Cox is often cleaner and more interpretable; Yeo-Johnson's piecewise form is harder to reason about, so use the simpler tool when it applies.
Log-transforming your target variable makes the model's raw predictions directly usable.
False. The model predicts in log space; you must inverse-transform with (plus a bias correction) to get back to original units.
A tree-based model like a decision tree benefits from log-transforming features.
False. Trees split on thresholds and only care about order; a monotonic transform gives identical splits, so it changes nothing for them. Transforms matter for models that assume a shape (linear/Gaussian), not for rank-based ones.
Square root is "gentler" than log for reducing right skew.
True. For , compresses large values more aggressively than ; sqrt preserves more of the original spacing, so it suits moderate skew.
The logit transform works on any positive number.
False. Logit is for proportions strictly inside . At or the fraction blows up to (division by zero or ), so bounded proportions only.
Adding a constant before logging shifts the data but never distorts its shape.
False. The choice of constant changes the shape near small values: vs pull the low end very differently. The constant is a modelling decision, not a free parameter.
Spot the error
transformed = np.log(prices) on a column that contains a 0.
Error: , poisoning the column with
-inf. Use np.log1p(prices) (which is ) so zeros map to instead of blowing up.Fit Box-Cox's on the entire dataset, then split into train/test.
Error: this is data leakage — the test set's distribution influenced . Split first, learn on train only, then apply that same to test.
"My target was log-transformed, so I'll report as the prediction."
Error: of the mean underestimates the true mean (Jensen's inequality). Add the bias correction , where is the residual variance in log space.
Run Box-Cox directly on temperature anomalies containing .
Error: Box-Cox needs ; a negative base with a fractional power is undefined. Use Yeo-Johnson, which mirrors the transform for .
"I'll scale with standardisation first, then log-transform."
Error: standardising creates negative values (mean-centred), breaking . Transform first to fix the shape, then scale the already-symmetric result.
" gives , so it's the reciprocal transform."
Half-error: Box-Cox at is , which is shifted — it preserves order (bigger → bigger output), whereas a bare reverses it. The and matter.
Why questions
Why divide by and subtract in Box-Cox instead of just using ?
To make the family continuous at : . Raw collapses to the constant at , leaving a hole exactly where log lives.
Why does log space turn multiplicative change into additive change?
Because . A ×2 jump ( or ) is the same additive step in log space, matching how we perceive proportional growth.
Why do we want skewness near zero after transforming?
A near-symmetric distribution better satisfies the Gaussian/linear assumptions of models like linear regression, and stops a few extreme values from dominating the fitted line.
Why does Yeo-Johnson use the exponent (not ) for negative inputs?
It's the mirror-image construction: it makes the negative branch match the positive branch's behaviour smoothly so the whole function is continuous and differentiable at — no kink where the pieces meet.
Why prefer transformation over just deleting outliers?
Deletion throws away real information and can bias the model; a transform keeps every point but pulls extreme values inward, respecting that they may be genuine, meaningful observations.
Why is sometimes preferred over natural log for interpretation?
Each unit in means an exact ×10 change, which humans read easily ("two units = 100×"), whereas each unit in is a × change that's harder to narrate.
Edge cases
What does do to an input of exactly ?
It maps to — the clean, defined anchor point. This is precisely why
log1p exists: to make zero a valid, well-behaved value.Box-Cox at on a value of — problem or fine?
Fine in principle (), but Box-Cox's MLE for still requires strictly positive data, so a single zero forces you to add a constant or switch to Yeo-Johnson.
Yeo-Johnson at exactly with — which branch, and is it smooth?
The branch gives ; the transform equals at and the two pieces meet with matching slope, so it passes through the origin smoothly.
A feature is constant (every value identical) — what happens under any power transform?
The output is still constant (just a shifted constant); skewness is undefined and the feature carries zero information, so no transform can help — drop it instead.
What if the "optimal" fitted comes out very close to ?
The data is telling you it's already near-symmetric; applying Box-Cox does almost nothing, so skip the transform to keep the feature interpretable and avoid needless complexity.
Can a log transform ever increase skew?
Yes — on left-skewed data (long tail toward small values), log stretches the low end further and can worsen the asymmetry. Log is a right-skew remedy; diagnose the direction first.
Recall Fast self-test
Name the only transform in this note valid on negative inputs. ::: Yeo-Johnson. What must you do to log-transformed predictions before reporting them? ::: Inverse-transform with , plus the bias correction. Why do decision trees ignore monotonic transforms? ::: They split on order/thresholds, which monotonic maps leave unchanged.