[Numpy-discussion] Functions that stores results without using the out keyword

Bruce Southey bsouthey@gmail....
Wed Aug 6 14:15:12 CDT 2008


Hi,
The following functions support a optional second argument that stores 
the result:
isfinite, isnan, isinf, isposinf and isneginf.

There may be other functions with the same behavior.

Is this usage equivalent to 'out' keyword that appears in other 
functions like mean?
If so, should these functions have the 'out' keyword?

Also, I am correct that the 'out' keyword must always be an array and 
not a scalar?
Numpy does returns the error 'TypeError: output must be an array' if a 
scalar is used.
I would like to be explicit in the documentation by stating that a 
scalar in any form can not be used.

Thanks
Bruce


import numpy as np
a = np.array([[1,2, np.nan],[3,4, np.inf]])
b=np.array([[0.0],[0.0]])
c = np.array([[2,2,2],[2,2,2]])
np.mean(a, axis=1, out=b)
#returns: array([[ NaN],
#                     [ Inf]])
np.isinf(a, c)
#returns: array([[0, 0, 0],
#                   [0, 0, 1]])
np.isnan(a, c)
#returns: array([[0, 0, 1],
#                    [0, 0, 0]])

d=a.mean()
np.mean(a, out=d)
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#  File "/usr/lib64/python2.5/site-packages/numpy/core/fromnumeric.py", 
line 1806, in mean
#    return mean(axis, dtype, out)
#TypeError: output must be an array





More information about the Numpy-discussion mailing list