possible bug with numpy.object_
is the following behaviour expected? or is this a bug with numpy.object_ ? I'm using numpy 1.0b1
print numpy.array([],numpy.float64).size0 print numpy.array([],numpy.object_).size1 Should the size of an array initialized from an empty list not always be 1 ? or am I just crazy?
Thanks,  Matt Knox _________________________________________________________________ Be one of the first to try Windows Live Mail. http://ideas.live.com/programpage.aspx?versionId=5d21c51ab16143149b0e491...
Matt Knox wrote:
is the following behaviour expected? or is this a bug with numpy.object_ ? I'm using numpy 1.0b1
print numpy.array([],numpy.float64).size 0
print numpy.array([],numpy.object_).size 1
Should the size of an array initialized from an empty list not always be 1 ? or am I just crazy?
Not in this case. Explictly creating an object array from any object (even the emptylist object) gives you a 0d array containing that object. When you explicitly create an object array a different section of code handles it and gives this result. This is a recent change, and I don't think this usecase was considered as a backward incompatibility (which I believe it is). Perhaps we should make it so array([],....) always returns an empty array. I'm not sure. Comments? Travis
On Tue, Aug 29, 2006 at 10:49:58AM 0600, Travis Oliphant wrote:
Matt Knox wrote:
is the following behaviour expected? or is this a bug with numpy.object_ ? I'm using numpy 1.0b1
print numpy.array([],numpy.float64).size 0
print numpy.array([],numpy.object_).size 1
Should the size of an array initialized from an empty list not always be 1 ? or am I just crazy?
Not in this case. Explictly creating an object array from any object (even the emptylist object) gives you a 0d array containing that object. When you explicitly create an object array a different section of code handles it and gives this result. This is a recent change, and I don't think this usecase was considered as a backward incompatibility (which I believe it is). Perhaps we should make it so array([],....) always returns an empty array. I'm not sure. Comments?
The current behaviour makes sense, but is maybe not consistent: N.array([],dtype=object).size == 1 N.array([[],[]],dtype=object).size == 2 Regards Stéfan
On 8/30/06, Stefan van der Walt <stefan@sun.ac.za> wrote:
The current behaviour makes sense, but is maybe not consistent:
N.array([],dtype=object).size == 1 N.array([[],[]],dtype=object).size == 2
Yes, including one more term in this check: In [5]: N.array([],dtype=object).size Out[5]: 1 In [6]: N.array([[]],dtype=object).size Out[6]: 1 In [7]: N.array([[],[]],dtype=object).size Out[7]: 2 Intuitively, I'd have expected the answers to be 0,1,2, instead of 1,1,2. Cheers, f
Fernando Perez wrote:
On 8/30/06, Stefan van der Walt <stefan@sun.ac.za> wrote:
The current behaviour makes sense, but is maybe not consistent:
N.array([],dtype=object).size == 1 N.array([[],[]],dtype=object).size == 2
Yes, including one more term in this check:
In [5]: N.array([],dtype=object).size Out[5]: 1
In [6]: N.array([[]],dtype=object).size Out[6]: 1
In [7]: N.array([[],[]],dtype=object).size Out[7]: 2
Intuitively, I'd have expected the answers to be 0,1,2, instead of 1,1,2.
What about N.array(3).size N.array([3]).size N.array([3,3]).size Essentially, the [] is being treated as an object when you explicitly ask for an object array in exactly the same way as 3 is being treated as a number in the default case. It's just that '[' ']' is "also" being used as the dimension delimiter and thus the confusion. It is consistent. It's a corner case, and I have no problem fixing the specialcase code running when dtype=object so that array([], dtype=object) returns an empty array, if that is the consensus. Travis
Cheers,
f
 Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with preintegrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.asus.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 _______________________________________________ Numpydiscussion mailing list Numpydiscussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpydiscussion
On 8/31/06, Travis Oliphant <oliphant.travis@ieee.org> wrote:
What about
N.array(3).size
N.array([3]).size
N.array([3,3]).size
Essentially, the [] is being treated as an object when you explicitly ask for an object array in exactly the same way as 3 is being treated as a number in the default case. It's just that '[' ']' is "also" being used as the dimension delimiter and thus the confusion.
It is consistent. It's a corner case, and I have no problem fixing the specialcase code running when dtype=object so that array([], dtype=object) returns an empty array, if that is the consensus.
I wasn't really complaining: these are corner cases I've never seen in real use, so I'm not really sure how critical it is to worry about them. Though I could see code which does automatic size/shape checks tripping on some of them. The shape tuples shed a bit of light on what's going on for the surprised (like myself): In [8]: N.array(3).shape Out[8]: () In [9]: N.array([3]).shape Out[9]: (1,) In [10]: N.array([3,3]).shape Out[10]: (2,) In [11]: N.array([]).shape Out[11]: (0,) In [12]: N.array([[]]).shape Out[12]: (1, 0) In [13]: N.array([[],[]]).shape Out[13]: (2, 0) I won't really vote for any changes one way or another, as far as I'm concerned it's one of those 'learn the library' things. I do realize that the nearambiguity between '[]' as an empty object and '[]' as the syntactic delimiter for a container makes this case a bit of a gotcha. I guess my only remaining question is: what is the difference between outputs #8 and #11 above? Is an empty shape tuple == array scalar, while a (0,) shape indicates a onedimensional array with no elements? If this interpretation is correct, what is the usage of the latter kind of object, given how it can't even be indexed? In [15]: N.array([])[0]  exceptions.IndexError Traceback (most recent call last) /home/fperez/research/code/mjmdim/pycode/<ipython console> IndexError: index out of bounds And is this really expected? In [18]: N.array([]).any() Out[18]: False In [19]: N.array([]).all() Out[19]: True It's a bit funny to have an array for which 'no elements are true' (any==false), yet 'all are true' (all==true), isn't it? Regards, f
On 8/31/06, Fernando Perez <fperez.net@gmail.com> wrote:
On 8/31/06, Travis Oliphant <oliphant.travis@ieee.org> wrote:
What about
N.array(3).size
N.array([3]).size
N.array([3,3]).size
Essentially, the [] is being treated as an object when you explicitly ask for an object array in exactly the same way as 3 is being treated as a number in the default case. It's just that '[' ']' is "also" being used as the dimension delimiter and thus the confusion.
It is consistent. It's a corner case, and I have no problem fixing the specialcase code running when dtype=object so that array([], dtype=object) returns an empty array, if that is the consensus.
I wasn't really complaining: these are corner cases I've never seen in real use, so I'm not really sure how critical it is to worry about them. Though I could see code which does automatic size/shape checks tripping on some of them. The shape tuples shed a bit of light on what's going on for the surprised (like myself):
In [8]: N.array(3).shape Out[8]: ()
In [9]: N.array([3]).shape Out[9]: (1,)
In [10]: N.array([3,3]).shape Out[10]: (2,)
In [11]: N.array([]).shape Out[11]: (0,)
In [12]: N.array([[]]).shape Out[12]: (1, 0)
In [13]: N.array([[],[]]).shape Out[13]: (2, 0)
I won't really vote for any changes one way or another, as far as I'm concerned it's one of those 'learn the library' things. I do realize that the nearambiguity between '[]' as an empty object and '[]' as the syntactic delimiter for a container makes this case a bit of a gotcha.
I guess my only remaining question is: what is the difference between outputs #8 and #11 above? Is an empty shape tuple == array scalar, while a (0,) shape indicates a onedimensional array with no elements? If this interpretation is correct, what is the usage of the latter kind of object, given how it can't even be indexed?
In [15]: N.array([])[0]
 exceptions.IndexError Traceback (most recent call last)
/home/fperez/research/code/mjmdim/pycode/<ipython console>
IndexError: index out of bounds
And is this really expected?
In [18]: N.array([]).any() Out[18]: False
This could be interpreted as : exists x, x element of array, s.t. x is true. In [19]: N.array([]).all()
Out[19]: True
Seems right: for all x, x element of array, x is true. It's a bit funny to have an array for which 'no elements are true'
(any==false), yet 'all are true' (all==true), isn't it?
Fun with empty sets! The question is, is a zero dimensional array an empty container or does it contain its value. The numpy choice of treating zero dimensional arrays as both empty containers and scalar values makes the determination a bit ambiguous although it is consistent with the indexing convention. Chuck
Fernando Perez wrote:
In [8]: N.array(3).shape Out[8]: ()
In [11]: N.array([]).shape Out[11]: (0,)
I guess my only remaining question is: what is the difference between outputs #8 and #11 above? Is an empty shape tuple == array scalar, while a (0,) shape indicates a onedimensional array with no elements? If this interpretation is correct, what is the usage of the latter kind of object, given how it can't even be indexed?
It can be iterated over (with zero iterations):
a = N.array([]) for i in a: ... print i ...
whereas the scalar can not:
b = N.array(3) b array(3) for i in b: ... print i ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: iteration over a scalar (0dim array)
Of course the scalar isn't empty, so ti's different in that way too. Can there be an empty scalar? It doesn't look like it. In fact, this looks like it may be a bug:
a = N.array([1,2,3]).sum(); a.shape; a.size; a () 1 6
That's what I'd expect, but what if you start with a (0,) array:
a = N.array([]).sum(); a.shape; a.size; a () 1 0
where did that zero come from?
N.__version__ '1.0b4'
Chris  Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception Chris.Barker@noaa.gov
Christopher Barker wrote:
Fernando Perez wrote:
In [8]: N.array(3).shape Out[8]: ()
In [11]: N.array([]).shape Out[11]: (0,)
I guess my only remaining question is: what is the difference between outputs #8 and #11 above? Is an empty shape tuple == array scalar, while a (0,) shape indicates a onedimensional array with no elements? If this interpretation is correct, what is the usage of the latter kind of object, given how it can't even be indexed?
It can be iterated over (with zero iterations):
a = N.array([]) for i in a: ... print i ...
whereas the scalar can not:
b = N.array(3) b array(3) for i in b: ... print i ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: iteration over a scalar (0dim array)
Of course the scalar isn't empty, so ti's different in that way too. Can there be an empty scalar? It doesn't look like it. In fact, this looks like it may be a bug:
a = N.array([1,2,3]).sum(); a.shape; a.size; a () 1 6
That's what I'd expect, but what if you start with a (0,) array:
a = N.array([]).sum(); a.shape; a.size; a () 1 0
where did that zero come from?
More or less from: >>> numpy.add.identity 0 All the ufuncs have an identity function that they use as a starting point for reduce and accumulate. Sum doesn't appear to actually ahve one, but since it's more or less the same as add.reduce it's probably good that it has the same behavior. Note that this also matches the behavior of python's built in sum, although there the identity is called 'start'. tim
N.__version__ '1.0b4'
Chris
Tim Hochberg wrote:
That's what I'd expect, but what if you start with a (0,) array:
a = N.array([]).sum(); a.shape; a.size; a () 1 0
where did that zero come from?
More or less from:
>>> numpy.add.identity 0
I'm not totally sure, but I think I'd rather it raise an exception. However, if it's not going to, then 0 is really the only reasonable answer. Chris  Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception Chris.Barker@noaa.gov
On 8/31/06, Christopher Barker <Chris.Barker@noaa.gov> wrote:
Fernando Perez wrote:
In [8]: N.array(3).shape Out[8]: ()
In [11]: N.array([]).shape Out[11]: (0,)
I guess my only remaining question is: what is the difference between outputs #8 and #11 above? Is an empty shape tuple == array scalar, while a (0,) shape indicates a onedimensional array with no elements? If this interpretation is correct, what is the usage of the latter kind of object, given how it can't even be indexed?
It can be iterated over (with zero iterations):
a = N.array([]) for i in a: ... print i ...
whereas the scalar can not:
b = N.array(3) b array(3) for i in b: ... print i ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: iteration over a scalar (0dim array)
Of course the scalar isn't empty, so ti's different in that way too. Can there be an empty scalar? It doesn't look like it. In fact, this looks like it may be a bug:
a = N.array([1,2,3]).sum(); a.shape; a.size; a () 1 6
That's what I'd expect, but what if you start with a (0,) array:
a = N.array([]).sum(); a.shape; a.size; a () 1 0
where did that zero come from?
I think that is correct, sums over empty sets are conventionally set to zero because they are conceived of as adding all the values in the set to zero. Typically this would be implemented as sum = 0 for i in set : sum += i; Chuck
participants (7)

Charles R Harris

Christopher Barker

Fernando Perez

Matt Knox

Stefan van der Walt

Tim Hochberg

Travis Oliphant