[Numpy-discussion] 2D array indexing

Julian Taylor jtaylor.debian at googlemail.com
Fri Feb 28 18:48:28 EST 2014


On 01.03.2014 00:32, Gökhan Sever wrote:
> 
> Hello,
> 
> Given this simple 2D array:
> 
> In [1]: np.arange(9).reshape((3,3))
> Out[1]:
> array([[0, 1, 2],
>        [3, 4, 5],
>        [6, 7, 8]])
> 
> In [2]: a = np.arange(9).reshape((3,3))
> 
> In [3]: a[:1:]
> Out[3]: array([[0, 1, 2]])
> 
> In [4]: a[:1,:]
> Out[4]: array([[0, 1, 2]])
> 
> Could you tell me why the last two indexing (note the comma!) results in
> the same array? Thanks.
> 


if you specify less indices than dimensions the latter dimensions are
implicitly all selected.
so these are identical for three dimensional arrays:
d = np.ones((3,3,3))
d[1]
d[1,:]
d[1,:,:]
d[1,...] (... or Ellipsis selects all remaining dimensions)

this only applies to latter dimensions in the shape, if you want to
select all earlier dimensions they have to be explicitly selected:
d[:,1] == d[:,1,:]
d[..., 1] = d[:,:,1]


as for :1: vs 1:, its standard python rules: start:stop:step, with all
three having defaults of 0:len(sequence):1



More information about the NumPy-Discussion mailing list