pfc
pfc

Reputation: 1910

why does numpy array return wrong shape of sub arrays when indexing

An example is shown as follows:

>>> import numpy as np
>>> a=np.zeros((288,512))
>>> x1,x2,y1,y2=0,16,0,16
>>> p=a[x1:x2][y1:y2]
>>> p.shape
(16, 512)
>>> p=a[x1:x2,y1:y2]
>>> p.shape

I try to query a patch from an array, ranging from columns 0 to 16, and rows 0 to 16. I index the array in two ways and get very different result. a[x1:x2][y1:y2] gives me the wrong result.

Why?

Thx for helping me!!!

Upvotes: 1

Views: 938

Answers (2)

Ricardo Erikson
Ricardo Erikson

Reputation: 485

When you do a[x1:x2][y1:y2], you are slicing by rows twice. That is, a[x1:x2] will give you a shape (16,512). The second slice operation in a[x1:x2][y1:y2] is slicing the result of the first operation and will give you the same result.

In the second case, when you do a[x1:x2,y1:y2], you are slicing by the two dimensions of your 2-dimensional array.

Important note: If you have a 2-dimensional array and you slice like this:

a = np.zeros((10,15))
a[1:3].shape

Output:
(2, 15)

you will slice only by rows. Your resulting array will have 2 rows and the total number of columns (15 columns). If you want to slice by rows and columns, you will have to use a[1:3, 1:3].

Upvotes: 3

jkr
jkr

Reputation: 19310

The two methods of indexing you tried are not equivalent. In the first one (a[x1:x2][y1:y2]), you are essentially indexing the first axis twice. In the second, you are indexing the first and second axes.

a[x1:x2][y1:y2] can be rewritten as

p = a[x1:x2] # result still has two dimensions
p = p[y1:y2]

You are first indexing 0:16 in the first dimension. Then you index 0:16 in the first dimension of the result of the previous operation (which will simply return the same as a[x1:x2] because x1==y1 and x2==y2).

In the second method, you index the first and second dimensions directly. I would not write it this way, but one could write it like this to contrast it with the first method:

a[x1:x2][:, y1:y2]

Upvotes: 1

Related Questions