简体   繁体   中英

numpy multi-dimensional selecton with 2D indexing

I am confused with below cases:

x = np.random.rand(1, 32, 3, 64);

print(x.shape)

#(1, 32, 3, 64)

Then I apply this selection.

y = x[np.arange(1)[:, None], :, 1,  np.arange(64)[None, :]]

I think i selected all rows in dimension 0, 1, 3 , the 1 in 2nd dimension should be exception.. but it gave

print(y.shape)

#(1, 64, 32)

for below case:

y = x[np.arange(1)[:, None], :, [1],  np.arange(64)[None, :]]

I think i selected all rows in 0, 1 dimension, and always use the 1st row in 2 dimension.

print(y.shape)

#(1, 64, 32)

Why the shape become this (1, 64, 32) in both cases?

And what is the difference of 1 and [1]?

Can you maybe give a bit more context on what you are trying to achieve? Is there a specific reason you are indexing with np.arange(1)[:, None] instead of just : ? eg is y = x[:,:,1,:] shape:(1,32,64) not what you are looking for?

The 1 vs. [1] indexing difference looks like it could be advanced indexing: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM