liveBook · Manning

Log transformation

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

A log transformation applies a logarithm to values, converting multiplicative relationships into additive ones and changing their scale.

A log transformation changes a numeric variable by applying a logarithm. It is a practical response to a strongly right-skewed target: a few very large values stretch the scale, compressing the bulk of the distribution into an unreadable lower range. For positive prices, no offset is needed; when zero or negative values occur, add a constant first to move values into the positive range. section 4.2.1

Use histograms and boxplots to decide whether the transformation helps. Applied to listing prices, the transformed histogram becomes more symmetric and the boxplot becomes easier to inspect. The transformation does not remove every extreme observation: outliers can remain in both tails, so it is a change of representation rather than a guarantee of clean data. section 4.2.1

Transform a positive price target

Use NumPy's log1p to transform price values and inspect the result. The paired plots compare the transformed histogram with a boxplot, making it easy to check whether the long right tail has become more manageable. section 4.2.1

np.log1p(data["price"]).hist(bins=20)
data[["price"]].apply(lambda x: np.log1p(x)).boxplot()

This listing applies np.log1p to price, then plots a histogram and boxplot of the transformed values. section 4.2.1

For a linear regression baseline, transforming a positive target can help avoid infeasible zero or negative predictions. It is one possible remedy, not a requirement for the baseline: the baseline's purpose is to reveal data challenges and provide a comparison for more sophisticated models. section 7.2.3

Log transformations also matter when interpreting SHAP output from a multiplicative model. To reconstruct an original prediction from those SHAP values, exponentiate the values and multiply them; the reconstruction may differ slightly because of approximation and small errors. section 7.3

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Explains target transformation through plots and NumPy code, while noting persistent outliers rather than deriving logarithms mathematically.
Examples
Demonstrates np.log1p on listing prices with histograms and boxplots, then reconstructs a SHAP-based prediction by exponentiating and multiplying values.
What it assumes
Assumes tabular price data and basic pandas/NumPy plotting; teaches the transformation as a practical response to skewness.
What you can do after
Apply it via NumPy to transform a positive price target and evaluate the distribution with histograms and boxplots.
How it connects
Connects it to right-skewed distributions, outliers, linear regression, SHAP values, and multiplicative models.

Also explained in

Compare how 2 books explain log transformation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.