[Numpy-discussion] Invalid value encoutered : how to prevent numpy.where to do this?

Eric Emsellem eric.emsellem at eso.org
Sat Jan 5 09:15:02 EST 2013


Dear all,

I have a code using lots of "numpy.where" to make some constrained 
calculations as in:

data = arange(10)
result = np.where(data == 0, 0., 1./data)

# or
data1 = arange(10)
data2 = arange(10)+1.0
result = np.where(data1 > data2, np.sqrt(data1-data2), np.sqrt(data2-data2))

which then produces warnings like:
/usr/bin/ipython:1: RuntimeWarning: invalid value encountered in sqrt

or for the first example:

/usr/bin/ipython:1: RuntimeWarning: divide by zero encountered in divide

How do I avoid these messages to appear?

I know that I could in principle use numpy.seterr. However, I do NOT 
want to remove these warnings for other potential divide/multiply/sqrt 
etc errors. Only when I am using a "where", to in fact avoid such 
warnings! Note that the warnings only happen once, but since I am going 
to release that code, I would like to avoid the user to get such 
messages which are irrelevant here (because I am testing, with the 
where, when NOT to divide by zero or take a sqrt of a negative number).

thanks!
Eric



More information about the NumPy-Discussion mailing list