[Numpy-discussion] Is this an indexing bug?

Travis Oliphant oliphant.travis at ieee.org
Wed Jun 20 14:23:43 EDT 2007


Stefan van der Walt wrote:
> On Tue, Jun 19, 2007 at 12:35:05PM +0200, Sturla Molden wrote:
>   
>> On 6/19/2007 12:14 PM, Sturla Molden wrote:
>>
>>     
>>> h[0,:,numpy.arange(14)] is a case of "sdvanced indexing". You can also 
>>> see that
>>>
>>>  >>> h[0,:,[0,1,2,3,4,5,6,7,8,9,10,11,12,13]].shape
>>> (14, 4)
>>>       
>> Another way to explain this is that numpy.arange(14) and 
>> [0,1,2,3,4,5,6,7,8,9,10,11,12,13] is a sequence (i.e. iterator).  So 
>> when NumPy iterates the sequence, the iterator yields a single integer, 
>> lets call it I. Using this integer as an index to h, gives a = h[0,:,I] 
>> which has shape=(4,). This gives us a sequence of arrays of length 4. In 
>>     
>
> If you follow this analogy,
>
> x = N.arange(100).reshape((10,10))
> x[:,N.arange(5)].shape
>
> should be (5, 10), while in reality it is (10, 5).
>   

No, in this case, there is no ambiguity regarding where to put the 
sub-space, so it is put in the "expected" position. 

It could be argued that when a single integer is used in one of the 
indexing dimensions then there is also no ambiguity --- but the indexing 
code does not check for that special case.

There is no bug here as far as I can tell.  It is just perhaps somewhat 
unexpected behavior of a general rule about how "indirect" or "advanced" 
indexing is handled. 

You can always do

h[0][:,arange(14)] 

to get the result you seem to want.


-Travis







> Cheers
> Stéfan
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
>   




More information about the NumPy-Discussion mailing list