Income, page views, city populations, transaction amounts — these are all right-skewed: most values are small and a few are enormous. A linear model fed that column spends all its attention on the handful of giants. Taking a logarithm compresses the long tail and pulls the distribution toward something symmetric.
The obvious problem is that log(0) is undefined, and columns like "number of purchases" are full of zeros. So the standard transform is log1p:
Task: write log_transform(values) returning the transformed values, each rounded to 4 decimal places.
0 or greater, so 1 + x is always at least 1 and the log is always defined and non-negative.0 maps to exactly 0.0 — which is the nice property that makes log1p the default choice rather than log(x + 1) written out by hand.Use math.log1p(x) rather than math.log(1 + x). For tiny values they differ: at x = 1e-16, computing 1 + x in floating point gives exactly 1.0, so log(1 + x) returns 0.0 and loses the value entirely, while log1p is built to stay accurate there.
What the transform buys you is visible in the last test: inputs spanning 0 to a million come out spanning 0 to about 13.8. Ratios become distances — a tenfold increase is always the same step of about 2.3, wherever it happens on the scale — which is exactly the shape a linear model can use.