Log transformation is one of the most common feature engineering techniques for handling right-skewed data. Features like income, price, page views, and population often span several orders of magnitude and have long right tails. Applying log(1+x) compresses the range, reduces the impact of outliers, and often makes the distribution more symmetric - all of which help linear models and gradient-based optimizers.
Given a list of non-negative values, apply the log1p transformation (natural logarithm of 1 plus x) to each value.
Using log(1+x) instead of log(x) handles the case where x = 0, since log(1) = 0.
Input:
values = [0, 1, 2, 3]
Output:
[0.0, 0.6931, 1.0986, 1.3863]
log(1+0) = 0, log(1+1) = 0.6931, log(1+2) = 1.0986, log(1+3) = 1.3863. The values are compressed into a smaller range.
Input:
values = [99, 999]
Output:
[4.6052, 6.9078]
log(100) = 4.6052, log(1000) = 6.9078. Values differing by 10x in the original space are only about 2.3 apart after transformation.
Python's math module provides math.log1p(x) which computes log(1+x) with better numerical precision for small x. Alternatively, use math.log(1 + x).
A simple list comprehension does the job: [math.log1p(v) for v in values]. Remember to import math.
Sign in to take notes on this problem
Accepts: array
Log transformation is one of the most common feature engineering techniques for handling right-skewed data. Features like income, price, page views, and population often span several orders of magnitude and have long right tails. Applying log(1+x) compresses the range, reduces the impact of outliers, and often makes the distribution more symmetric - all of which help linear models and gradient-based optimizers.
Given a list of non-negative values, apply the log1p transformation (natural logarithm of 1 plus x) to each value.
Using log(1+x) instead of log(x) handles the case where x = 0, since log(1) = 0.
Input:
values = [0, 1, 2, 3]
Output:
[0.0, 0.6931, 1.0986, 1.3863]
log(1+0) = 0, log(1+1) = 0.6931, log(1+2) = 1.0986, log(1+3) = 1.3863. The values are compressed into a smaller range.
Input:
values = [99, 999]
Output:
[4.6052, 6.9078]
log(100) = 4.6052, log(1000) = 6.9078. Values differing by 10x in the original space are only about 2.3 apart after transformation.
Python's math module provides math.log1p(x) which computes log(1+x) with better numerical precision for small x. Alternatively, use math.log(1 + x).
A simple list comprehension does the job: [math.log1p(v) for v in values]. Remember to import math.
Sign in to take notes on this problem
Accepts: array