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:
- Defined for x \geq 0
- \log(1 + 0) = 0 (zero maps to zero)
- For small x, \log(1 + x) \approx x
- For large x, behavior approaches standard log
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):
- Long right tail with extreme outliers
- Mean pulled above median
- Most values clustered at low end
After log transform:
- More symmetric, often approximately normal
- Extreme values compressed
- Better spread of values across the range
Example: Income data
- Raw: Most people earn in the range of 30K- few earn $1M+
- Log-transformed: Approximately normal, billionaires no longer dominate
Worked Example
Original data: [1, 10, 100, 1000, 10000]
Log base 10 transform:
- \log_{10}(1) = 0
- \log_{10}(10) = 1
- \log_{10}(100) = 2
- \log_{10}(1000) = 3
- \log_{10}(10000) = 4
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:
- Strictly positive data (or use log1p for non-negative)
- Right-skewed distributions
- Data with exponential growth patterns
- Ratios and proportions
- Count data with high variance
- Financial returns, prices, volumes
Poor candidates:
- Data with zeros (use log1p) or negative values
- Already normally distributed data
- Data where linear relationships are appropriate
- Categorical or ordinal data
Detecting When Log Transform Helps
Visual inspection:
- Histogram shows right skew before, symmetric after
- Q-Q plot more linear after transformation
Statistical tests:
- Compare skewness before and after (should approach 0)
- Shapiro-Wilk test for normality improvement
Model performance:
- Compare R-squared or prediction error with and without transform
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:
- Signed log: \text{sign}(x) \cdot \log(1 + |x|)
- Box-Cox transform (more general)
- Yeo-Johnson transform (handles negatives)
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
Economics: GDP, income, prices, stock returns are often modeled in log scale
Biology: Population growth, enzyme kinetics, dose-response relationships
Information Theory: Entropy calculations, information gain
Signal Processing: Decibel scale, perception of loudness/brightness
Machine Learning: Feature engineering for tree models, neural network inputs
Natural Language Processing: Word frequencies follow Zipf's law (log-linear)
Network Analysis: Node degree distributions often log-normal
Epidemiology: Disease spread often exponential, log transform linearizes
Chemistry: pH scale, reaction rates
Astronomy: Magnitude scale for star brightness
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
- Use math.log1p to compute the transformation directly.
- Round each transformed value while building the result list.
Requirements
- Apply the natural logarithm of 1 + x to every value.
- Round every transformed value to four decimal places.
- Preserve the original value order.
- Return a list of floats.
Constraints
- Every input value is nonnegative.
- values is nonempty.
- Time limit: 300 ms.
Starter Code
import math
def log_transform(values: list) -> list:
"""
Returns the log1p-transformed values rounded to four decimals.
"""
# Write code here
passTest Cases
| Case | Matches | |
|---|---|---|
| Basic | — | public |
| Large | — | public |