EasyFeature Engineering

Log Transform

Feature Engineering · Data Processing

Easy

Problem

The log1p transformation compresses the range of nonnegative data while handling zero naturally. Apply the natural logarithm of one plus each value:

y_i = \ln(1 + x_i)

Here, x_i is an input value and y_i is its transformed value. Round each result to four decimal places and return the transformed values as a list.

Theory

A log transform applies the logarithm function to data values, converting multiplicative relationships into additive ones. It is one of the most common data transformations in statistics and machine learning, particularly useful for right-skewed distributions and data spanning multiple orders of magnitude.


Why Use Log Transforms?

Reducing skewness: Many real-world distributions (income, population, prices) are heavily right-skewed with long tails. Log transform compresses the right tail and expands the left, often producing approximately normal distributions.

Stabilizing variance: When variance increases with the mean (heteroscedasticity), log transform can create more constant variance across the range of values.

Handling multiplicative effects: When features have multiplicative rather than additive effects on the outcome, log transform converts them to additive effects suitable for linear models.

Spanning orders of magnitude: Data ranging from 1 to 1,000,000 becomes 0 to 6 after log10 transform, making it easier to visualize and process.


Mathematical Definition

The natural logarithm (base e) is most common:

y = \ln(x) = \log_e(x)

Other bases are also used:

\log_{10}(x) = \frac{\ln(x)}{\ln(10)} \approx \frac{\ln(x)}{2.303}

\log_2(x) = \frac{\ln(x)}{\ln(2)} \approx \frac{\ln(x)}{0.693}

Key property: The choice of base only changes the scale by a constant factor. The shape of the transformed distribution is identical regardless of base.


The Log1p Transform

For data that includes zeros or values close to zero, the standard log is undefined or produces extreme negative values. The log1p transform addresses this:

y = \log(1 + x)

Properties:

Numerical stability: For very small x, computing 1 + x can lose precision. Implementations typically use specialized algorithms to maintain accuracy.


Inverse Transform

To convert back to the original scale:

x = e^y \quad \text{(inverse of natural log)}

x = 10^y \quad \text{(inverse of log base 10)}

For log1p:

x = e^y - 1 \quad \text{(expm1 function)}

Important: After modeling in log space, predictions must be inverse-transformed for interpretation. The inverse of mean(log(x)) is NOT mean(x).


Effect on Distribution Shape

Before log transform (right-skewed):

After log transform:

Example: Income data


Worked Example

Original data: [1, 10, 100, 1000, 10000]

Log base 10 transform:

Result: [0, 1, 2, 3, 4]

The data spanning 4 orders of magnitude is now evenly spaced integers.


When to Apply Log Transform

Good candidates:

Poor candidates:


Detecting When Log Transform Helps

Visual inspection:

Statistical tests:

Model performance:


Handling Zeros and Negative Values

Zeros: Use log1p transform: \log(1 + x)

Small positive constant: Add a small value before log: \log(x + \epsilon) where \epsilon might be 1 or the minimum positive value in the data

Negative values: Not suitable for standard log transform. Consider:


Log Transform in Linear Regression

Log-transformed dependent variable:

\log(y) = \beta_0 + \beta_1 x

Interpretation: A one-unit increase in x corresponds to a multiplicative change of e^{\beta_1} in y.

Log-transformed independent variable:

y = \beta_0 + \beta_1 \log(x)

Interpretation: A 1% increase in x corresponds to an additive change of \beta_1 / 100 in y.

Log-log model:

\log(y) = \beta_0 + \beta_1 \log(x)

Interpretation: \beta_1 is the elasticity - a 1% increase in $$ corresponds to a \beta_1% change in y.


Common Pitfalls

Forgetting to inverse transform: Predictions in log space must be converted back for interpretation.

Jensen's inequality: The mean of logged values is NOT the log of the mean. E[\log(X)] \neq \log(E[X])

Interpretation errors: Coefficients in log-transformed models represent multiplicative, not additive, effects.

Over-application: Not all skewed data benefits from log transform. Always compare model performance.


Where Log Transforms Show Up

Examples

Example 1

Input
values = [0, 1, 2, 3]
Output
[0.0, 0.6931, 1.0986, 1.3863]
Explanation
Adding one makes the zero input valid, and the natural logarithm compresses the remaining values.

Example 2

Input
values = [99, 999]
Output
[4.6052, 6.9078]

Hints

  1. Use math.log1p to compute the transformation directly.
  2. Round each transformed value while building the result list.

Requirements

Constraints

Starter Code

import math

def log_transform(values: list) -> list:
    """
    Returns the log1p-transformed values rounded to four decimals.
    """
    # Write code here
    pass

Test Cases

CaseMatches
Basicpublic
Largepublic