I'd like to take the natural logarithm of a numpy array but not calculate the logarithm of 0-entries at all. I.e. I'd like to implement the convention log(0)=0 over numpy arrays.
import numpy as np
import timeit
foo = np.random.rand(500)
%timeit np.log(foo)
%timeit np.log(foo,where=foo>0)
For the first call, this yields
The slowest run took 12.63 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: 2.06 µs per loop
For the second call, we get
The slowest run took 8.35 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: 4.31 µs per loop
So avoiding the 0's (even if there are no zeros in the array in this case) is much more expensive. This is clearly different if we look at sparse arrays, but is there a more efficient way to avoid zeros even in the non-sparse case?