numpy.where behavior

Robert Kern robert.kern at
Mon Nov 13 19:42:40 CST 2006

vallis.35530053 at wrote:
> ? I thought that the point of where was
> that the second expression is never used for the elements where the condition
> evaluates true. 

It is not used, but the expression still gets evaluated. There's really no way
around that.

> If this is the desired behavior, is there a way to suppress
> the warning?

In [1]: from numpy import *

In [2]: a = zeros(3)

In [3]: 1/a
Warning: divide by zero encountered in divide
Warning: invalid value encountered in double_scalars
Out[3]: array([              inf,               inf,               inf])

In [4]: seterr(divide='ignore', invalid='ignore')
Out[4]: {'divide': 'print', 'invalid': 'print', 'over': 'print', 'under': 'ignore'}

In [5]: 1/a
Out[5]: array([              inf,               inf,               inf])

In [6]: seterr?
Type:           function
Base Class:     <type 'function'>
Namespace:      Interactive
Definition:     seterr(all=None, divide=None, over=None, under=None, invalid=None)
    Set how floating-point errors are handled.

    Valid values for each type of error are the strings
    "ignore", "warn", "raise", and "call". Returns the old settings.
    If 'all' is specified, values that are not otherwise specified
    will be set to 'all', otherwise they will retain their old

    Note that operations on integer scalar types (such as int16) are
    handled like floating point, and are affected by these settings.


    >>> seterr(over='raise')
    {'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'}
    >>> seterr(all='warn', over='raise')
    {'over': 'raise', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'}
    >>> int16(32000) * int16(3)
    Traceback (most recent call last):
          File "<stdin>", line 1, in ?
    FloatingPointError: overflow encountered in short_scalars
    >>> seterr(all='ignore')
    {'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 'ignore'}

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

More information about the Numpy-discussion mailing list