new warning in 1.5.0: divide by zero encountered in log
Hi, I recently upgraded to numpy 1.5.0 and now I get warnings when I take the logarithm of 0. In [5]: np.log(0.) Warning: divide by zero encountered in log Out[5]: -inf I want to evaluate x * log(x) where its value is defined as 0 when x=0 so I have the following function: def safe_x_log_x(x): "@return: x log(x) but replaces -inf with 0." l = np.log(x) result = x * l result[np.isneginf(l)] = 0. return result Is there a better way of doing this that won't give me all these warnings? Can I turn the warnings off? Thanks, John.
On 9/8/2010 6:38 AM, John Reid wrote:
def safe_x_log_x(x): "@return: x log(x) but replaces -inf with 0." l = np.log(x) result = x * l result[np.isneginf(l)] = 0. return result
Assuming x is known to contain nonnegative floats: def safe_x_log_x(x): x = np.asarray(x) l = np.zeros_like(x) l[x>0] = np.log(x[x>0]) return x * l hth, Alan Isaac
On Wed, Sep 8, 2010 at 4:38 AM, John Reid <j.reid@mail.cryst.bbk.ac.uk>wrote:
Hi,
I recently upgraded to numpy 1.5.0 and now I get warnings when I take the logarithm of 0.
In [5]: np.log(0.) Warning: divide by zero encountered in log Out[5]: -inf
I want to evaluate x * log(x) where its value is defined as 0 when x=0 so I have the following function:
def safe_x_log_x(x): "@return: x log(x) but replaces -inf with 0." l = np.log(x) result = x * l result[np.isneginf(l)] = 0. return result
Is there a better way of doing this that won't give me all these warnings? Can I turn the warnings off?
In [1]: log(0) Warning: divide by zero encountered in log Out[1]: -inf In [2]: olderr = np.seterr(divide='ignore') In [3]: log(0) Out[3]: -inf Chuck
participants (3)
-
Alan G Isaac -
Charles R Harris -
John Reid