[Numpy-discussion] possible bug with numpy.object_

Tim Hochberg tim.hochberg at ieee.org
Thu Aug 31 11:57:25 CDT 2006


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 one-dimensional 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 (0-dim 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
>
>
>
>   






More information about the Numpy-discussion mailing list