[Numpy-discussion] Is this an indexing bug?

Sturla Molden sturla@molden...
Tue Jun 19 05:14:22 CDT 2007


On 6/19/2007 12:19 PM, Sven Schreiber wrote:

> To be more specific, I would expect shape==(4,14).


 >>> h = numpy.zeros((1,4,14))
 >>> h[0,:,numpy.arange(14)].shape
(14, 4)
 >>> h[0,:,:].shape
(4, 14)
 >>>


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)

Citing from Travis' book, page 83:

"Example 2: Now let X.shape be (10,20,30,40,50) and suppose ind1 and 
ind2 are broadcastable to the shape (2,3,4). Then X[:,ind1,ind2] has 
shape (10,2,3,4,40,50) because the (20,30)-shaped subspace from X has 
been replaced with the (2,3,4) subspace from the indices. However, 
X[:,ind1,:,ind2,:] has shape (2,3,4,10,30,50) because there is no 
unambiguous place to drop in the indexing subspace, thus it is tacked-on 
to the beginning. It is always possible to use .transpose() to move the 
sups pace anywhere desired. This example cannot be replicated using take."


So I think this strange behaviour is actually correct.



Sturla Molden




More information about the Numpy-discussion mailing list