[Numpy-discussion] Is this an indexing bug?

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

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 
other words,

 >>> a = numpy.zeros(4)
 >>> numpy.array([a,a,a,a,a,a,a,a,a,a,a,a,a,a]).shape
(14, 4)

That is analogous to

array([(0,0,0,0), (0,0,0,0), ...., (0,0,0,0)])

Sturla Molden

More information about the Numpy-discussion mailing list