liveBook · Manning

Logarithmic scale

As explained in Statistics Every Programmer Needs

A logarithmic transformation replaces each value with its logarithm to compress scale differences and reduce positive skewness.

A logarithmic transformation replaces each value with its logarithm, compressing differences across several orders of magnitude and helping reduce right skewness. Use it when preparing variables for linear regression, but interpret the fitted relationship on the transformed scale rather than automatically on the original scale. section 4.1.3

Apply a logarithmic scale when:

  • The data spans several orders of magnitude and its range needs compression. section 4.1.3
  • The distribution is right-skewed and transformation may help it approach normality. section 4.1.3
  • A linear model requires transformed data to better meet its assumptions, while alternative nonlinear models remain worth considering. section 4.1.3

Transforming a regression variable

For a variable x in a pandas data frame df, apply df['x'] = np.log(df['x']) or df['x'] = np.log10(df['x']). The resulting regression describes associations using the transformed x values. Compare the transformed model with alternatives and inspect the influence of outliers rather than treating improved fit alone as sufficient evidence. section 4.1.3

df['x'] = np.log(df['x'])

Use the compact NumPy listing to apply a natural-log transformation directly to a data-frame column. section 4.1.3

Logarithmic scales also describe Benford distributions: leading digits are not equally likely, with 1 occurring about 30% of the time and 9 occurring below 5%. section 12.1

Computing a Benford distribution

For digits d from 1 through 9, compute round(np.log10(1 + (1 / d)), 3). The resulting values begin 0.301 for 1 and 0.046 for 9, and sum to 1.0. Plotting these probabilities distinguishes the logarithmic pattern from uniform or random distributions. section 12.1 section 12.3.3

How Statistics Every Programmer Needs covers it

How deep it goes
Explains use cases, transformed-scale interpretation, NumPy implementation, probability calculation, and distribution plots, but does not develop alternative nonlinear models.
Examples
Demonstrates data-frame transformation, Benford probabilities for digits 1–9, and plots contrasting logarithmic, uniform, and random distributions.
What it assumes
Assumes basic data frames, logarithms, probability distributions, and linear regression while introducing practical transformation choices and cautions.
What you can do after
Apply logarithms with NumPy, compute Benford probabilities, and interpret transformed-scale results cautiously in regression.
How it connects
Connects logarithmic scales with linear regression, normal distribution, right skewness, outliers, Benford's law, and probability distributions.

Also explained in

Compare how 3 books explain logarithmic scale →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.