简体   繁体   English

带有二维索引的numpy多维选择器

[英]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 我认为我选择了维度0、1、3的所有行,第二维度的1应该例外。

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. 我认为我选择了0、1维的所有行,并且始终使用2维的第一行。

print(y.shape)

#(1, 64, 32)

Why the shape become this (1, 64, 32) in both cases? 为什么在两种情况下形状都变为(1、64、32)?

And what is the difference of 1 and [1]? 1和[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 : ? 你有什么用索引特殊原因np.arange(1)[:, None] ,而不只是: eg is y = x[:,:,1,:] shape:(1,32,64) not what you are looking for? 例如y = x[:,:,1,:] shape:(1,32,64)不是您要的内容吗?

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 1索引与[1]索引之间的差异看起来可能是高级索引: https : //docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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