Skip to content
tezvyn:
Menu

Log Transformation: Taming Skewed Data for Better Models

Source: Wikipedia: Log transformation (statistics)MediumHow cards are made

A log transform tames skewed data by compressing large values and spreading out small ones. It's used on data like income or web traffic to help it meet the assumptions of linear models. The footgun: it fails on zero or negative values.

Why it exists

Many statistical models, like linear regression, work best when data follows a normal (bell-curve) distribution. Real-world data, such as income levels or city populations, is often heavily right-skewed with a long tail of very high values, which violates this assumption and can lead to poor model performance.

The mental model

Think of a log transform as a data compressor for the high end and an expander for the low end. It pulls in extreme outliers and spreads out values that are clustered close to zero. The goal is to turn a skewed distribution into a more symmetric, manageable shape that better fits the assumptions of a statistical model.

How it works

For every positive data point x in a dataset, you replace it with its logarithm, y = log(x). You can use any base, like the natural log (ln) or base 10, as long as you are consistent. This transformation has a dramatic effect on large numbers: for log base 10, a value of 100 becomes 2, 1,000 becomes 3, and 1,000,000 becomes 6. The absolute differences between large numbers are drastically reduced, re-scaling the entire dataset.

When to use it

Apply a log transform to right-skewed, positive-only data before feeding it into a linear model. It's a standard step in feature engineering when dealing with quantities that grow multiplicatively, like financial data or population counts. The transformation helps satisfy model assumptions like normality of residuals and homoscedasticity (constant variance).

When not to use it

Never apply a log transform directly to data containing zeros or negative numbers, as the logarithm is undefined for non-positive values. A common workaround is the log(x + 1) transform, but be aware this changes the data's properties. It is also counterproductive on data that is already symmetric or is left-skewed, as it will distort the distribution undesirably.

One canonical example

Consider a dataset of US household incomes. Most households earn between 40k-150k, but a small number of billionaires create an extreme right skew. Applying a log transform would pull in the outlier incomes, making the distribution of log(income) look much more like a bell curve, which is far more suitable for statistical analysis and modeling.

Interview question

Which scenario presents a fundamental mathematical barrier to directly applying a standard log transformation to a dataset?

  • a.The data represents quantities that grow multiplicatively, like financial data.
  • b.The data is heavily right-skewed, with many large outlier values.
  • c.The dataset includes observations with values of zero or negative numbers.Correct
  • d.The data already follows a normal (bell-curve) distribution.
Why?

A standard log transformation is mathematically undefined for non-positive values (zero or negative), making its direct application impossible in such cases. While applying it to already normal data (Option D) is counterproductive, it is not a fundamental mathematical barrier like the presence of non-positive values.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on data science — each one lists the topics its interview covers.

See open roles