I just came across one of these Kernels and couldn't understand what does numpy.log1p()
do in the third pipeline of this code (House Prediction dataset in Kaggle).
Numpy documentation said
Returns:
- An array with natural logarithmic value of x + 1
- where x belongs to all elements of input array.
What is the purpose of finding log with one added while finding skewness of original and transformed array of same features? What does it actually do?
log1p() function is used to get the natural logarithm of 1+x (base e); it accepts a number and returns the natural logarithm of 1+number on the base e.
log1p() The Math.log1p() function returns the natural logarithm (base e ) of 1 + x , where x is the argument.
exp(x) - 1, the inverse of log1p. For real-valued input, log1p is accurate also for x so small that 1 + x == 1 in floating-point accuracy. Logarithm is a multivalued function: for each x there is an infinite number of z such that exp(z) = 1 + x.
expm1(array, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : This mathematical function helps user to calculate exponential of all the elements subtracting 1 from all the input array elements.
The NumPy docs give a hint:
For real-valued input,
log1p
is accurate also forx
so small that1 + x == 1
in floating-point accuracy.
So for example let's add a tiny non-zero number and 1.0
. Rounding errors make it a 1.0
.
>>> 1e-100 == 0.0 False >>> 1e-100 + 1.0 == 1.0 True
If we try to take the log
of that incorrect sum, we get an incorrect result (compare to WolframAlpha):
>>> np.log(1e-100 + 1) 0.0
But if we use log1p()
, we get the correct result
>>> np.log1p(1e-100) 1e-100
The same principle holds for exp1m()
and logaddexp()
: The're more accurate for small x
.
If x is in range 0...+Inf then it will never cause an error (as we know log(0) would cause an error).
Not always the best choice, because as you see you will lose a big curve before x = 0 that is one of the best things about log function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With