Log Transform
Log Transform
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.
Algorithm
yi=ln(1+xi)Using log(1+x) instead of log(x) handles the case where x = 0, since log(1) = 0.
Examples
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.
Hint 1
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).
Hint 2
A simple list comprehension does the job: [math.log1p(v) for v in values]. Remember to import math.
Requirements
- Apply the natural logarithm of (1 + x) to each value
- All input values are non-negative
- log(1 + 0) = 0 handles the zero case naturally
- Return a list of floats
Constraints
- All values are non-negative (>= 0)
- Use the natural logarithm (base e)
- Return a list of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Log Transform
Log Transform
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.
Algorithm
yi=ln(1+xi)Using log(1+x) instead of log(x) handles the case where x = 0, since log(1) = 0.
Examples
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.
Hint 1
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).
Hint 2
A simple list comprehension does the job: [math.log1p(v) for v in values]. Remember to import math.
Requirements
- Apply the natural logarithm of (1 + x) to each value
- All input values are non-negative
- log(1 + 0) = 0 handles the zero case naturally
- Return a list of floats
Constraints
- All values are non-negative (>= 0)
- Use the natural logarithm (base e)
- Return a list of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array