[Numpy-discussion] numpy.where behavior

2006-11-13 Thread vallis . 35530053
Using numpy 1.0, why does



 a = numpy.array([0.0,1.0,2.0],'d')

 numpy.where(a
== 0.0,1,1/a)



give the correct result, but with the warning Warning: divide
by zero encountered in divide?



? I thought that the point of where was
that the second expression is never used for the elements where the condition
evaluates true. 



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



Thanks!



Michele

-
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
http://sel.as-us.falkag.net/sel?cmd=lnkkid=120709bid=263057dat=121642
___
Numpy-discussion mailing list
Numpy-discussion@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy.where behavior

2006-11-13 Thread Robert Kern
[EMAIL PROTECTED] 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
File:
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/numpy-1.0.1.dev3432-py2.5-macosx-10.4-i386.egg
/numpy/core/numeric.py
Definition: seterr(all=None, divide=None, over=None, under=None, 
invalid=None)
Docstring:
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
values.

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

Example:

 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
http://sel.as-us.falkag.net/sel?cmd=lnkkid=120709bid=263057dat=121642
___
Numpy-discussion mailing list
Numpy-discussion@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy.where behavior

2006-11-13 Thread Tim Hochberg
[EMAIL PROTECTED] wrote:
 Using numpy 1.0, why does



   
 a = numpy.array([0.0,1.0,2.0],'d')
 

   
 numpy.where(a
 
 == 0.0,1,1/a)



 give the correct result, but with the warning Warning: divide
 by zero encountered in divide?



 ? I thought that the point of where was
 that the second expression is never used for the elements where the condition
 evaluates true. 



 If this is the desired behavior, is there a way to suppress
 the warning?
   
Robert Kern has already pointed you to seterr. If you are using Python 
2.5, you also have the option using the with statement, which is more 
convenient if you want to temporarily change the error state. You'll 
need a from __future__ import with_statement at the top of your file. 
Then you can temporarily disable errors as shown:

  a = zeros([3])
  b = 1/a  # This will warn
Warning: divide by zero encountered in divide
  with errstate(divide='ignore'): # But this will not
... c = 1/a
...
  d = 1/a # And this will warn again since the error state is
restored when we exit the block
Warning: divide by zero encountered in divide


Another little tidbit: this is not as general as where, and could 
probably be considered a little too clever to be clear, but:

b = 1 / (a + (a==0.0))

is faster than using where in this particular case and sidesteps the 
divide by zero issue altogether.

-tim



-
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
http://sel.as-us.falkag.net/sel?cmd=lnkkid=120709bid=263057dat=121642
___
Numpy-discussion mailing list
Numpy-discussion@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy.where behavior

2006-11-13 Thread Paul Dubois
Unfortunately, where does not have the behavior of not evaluating the second argument where the first one is true. That would be nice (if the speed were ok) but it isn't possible unless where is built into the language, since where doesn't even get called until the arguments have all been calculated. It was intended as having a different use than avoiding zero-divide. The ma package can calculate 1/a without problem, resulting in masked results where a is 
0.0.I put where into numeric after it had proved invaluable in Basis, even though it has this limitation; it takes care of doing both merge and compress.On 13 Nov 2006 20:02:31 -0800, 
Tim Hochberg [EMAIL PROTECTED] wrote:
[EMAIL PROTECTED] wrote: Using numpy 1.0, why does a = numpy.array([0.0,1.0,2.0],'d')
 numpy.where(a == 0.0,1,1/a) give the correct result, but with the warning Warning: divide by zero encountered in divide?
 ? I thought that the point of where was that the second _expression_ is never used for the elements where the condition evaluates true. If this is the desired behavior, is there a way to suppress
 the warning?Robert Kern has already pointed you to seterr. If you are using Python2.5, you also have the option using the with statement, which is moreconvenient if you want to temporarily change the error state. You'll
need a from __future__ import with_statement at the top of your file.Then you can temporarily disable errors as shown:  a = zeros([3])  b = 1/a# This will warn
Warning: divide by zero encountered in divide  with errstate(divide='ignore'): # But this will not... c = 1/a...  d = 1/a # And this will warn again since the error state is
restored when we exit the blockWarning: divide by zero encountered in divideAnother little tidbit: this is not as general as where, and couldprobably be considered a little too clever to be clear, but:
b = 1 / (a + (a==0.0))is faster than using where in this particular case and sidesteps thedivide by zero issue altogether.-tim-
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 easierDownload IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnkkid=120709bid=263057dat=121642___
Numpy-discussion mailing listNumpy-discussion@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/numpy-discussion

-
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
http://sel.as-us.falkag.net/sel?cmd=lnkkid=120709bid=263057dat=121642___
Numpy-discussion mailing list
Numpy-discussion@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy.where behavior

2006-11-13 Thread Robert Kern
Tim Hochberg wrote:
 Another little tidbit: this is not as general as where, and could 
 probably be considered a little too clever to be clear, but:
 
 b = 1 / (a + (a==0.0))
 
 is faster than using where in this particular case and sidesteps the 
 divide by zero issue altogether.

A less clever approach that does much the same thing:

  b = 1.0 / where(a==0, 1.0, a)

-- 
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
http://sel.as-us.falkag.net/sel?cmd=lnkkid=120709bid=263057dat=121642
___
Numpy-discussion mailing list
Numpy-discussion@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion