Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore divide by 0 warning in NumPy

I have a function for statistic issues:

import numpy as np from scipy.special import gamma as Gamma  def Foo(xdata):     ...     return x1 * (                  ( #R is a numpy vector                   ( ((R - x2)/beta) ** (x3 -1) ) *                    ( np.exp( - ((R - x2) / x4) ) ) /                   ( x4 * Gamma(x3))                  ).real                 ) 

Sometimes I get from the shell the following warning:

RuntimeWarning: divide by zero encountered in... 

I use the numpy isinf function to correct the results of the function in other files, so I do not need this warning.

Is there a way to ignore the message? In other words, I do not want the shell to print this message.

I do not want to disable all python warnings, just this one.

like image 323
overcomer Avatar asked Apr 29 '15 17:04

overcomer


People also ask

How do you ignore divide by zero in Python?

In Python, we use a try block that contains a return statement to divide 2 numbers. If there is no division by zero error, then it will return the result. Otherwise, the except line will check if the specified exception name is a match, and then it will execute the code under the except block.

How does NumPy handle division by zero?

Behavior on division by zero can be changed using seterr. When both x1 and x2 are of an integer type, divide will return integers and throw away the fractional part. Moreover, division by zero always yields zero in integer arithmetic.

How can you prevent Runtimewarning divide by zero encountered in log?

I solved this by finding the lowest non-zero number in the array and replacing all zeroes by a number lower than the lowest :p.

How to disable zero division warning in NumPy?

You can disable the warning with numpy.seterr. Put this before the possible division by zero: That'll disable zero division warnings globally. If you just want to disable them for a little bit, you can use numpy.errstate in a with clause:

How to ignore NaN warning in NumPy?

If a NaN is expected the warning can be ignored using np.errstate. Show activity on this post. You could also use numpy.divide for division. That way you don't have to explicitly disable warnings.

What is the difference between'out'and'where'in NumPy?

This acts slightly differently than the np.where function, in that it only evaluates the function "where" the mask is true. When the mask is False, it doesn't change the value, so using the "out" argument allows us to preallocate any default we want.

What is the fastest way to sort an array in NumPy?

Note that if you only want the top ten values from an NumPy array, using the np.argpartition function may be quicker than fully sorting the entire array, especially for large arrays: This shows np.argpartition is quicker for even only moderately large arrays:


1 Answers

You can disable the warning with numpy.seterr. Put this before the possible division by zero:

np.seterr(divide='ignore') 

That'll disable zero division warnings globally. If you just want to disable them for a little bit, you can use numpy.errstate in a with clause:

with np.errstate(divide='ignore'):     # some code here 

For a zero by zero division (undetermined, results in a NaN), the error behaviour has changed with numpy version 1.12.0: this is now considered "invalid", while previously it was "divide".

Thus, if there is a chance you your numerator could be zero as well, use

np.seterr(divide='ignore', invalid='ignore') 

or

with np.errstate(divide='ignore', invalid='ignore'):     # some code here 

See the "Compatibility" section in the release notes, last paragraph before the "New Features" section:

Comparing NaN floating point numbers now raises the invalid runtime warning. If a NaN is expected the warning can be ignored using np.errstate.

like image 86
dddsnn Avatar answered Sep 25 '22 22:09

dddsnn