numpy数组索引中的隐式转置 [英] Implicit transposing in numpy array indexing
问题描述
我遇到了一个奇怪的问题:
I came across a weird problem:
from numpy import zeros, arange
aa = zeros([1, 3, 10])
aa[0, :, arange(5)].shape
运行此代码可以得到(5,3)
,但是我希望得到(3,5)
.
Running this gives me (5,3)
, but I'm expecting (3,5)
.
但是,运行以下命令可以使我得到(3,5)
.
However, running the following gives me (3,5)
as expected.
aa = zeros([3, 10])
aa[:, arange(5)]
作为我的程序的一部分,此问题很容易解决,但这完全破坏了我的信念.
This is easy to fix as part of my program, but it completely ruined my belief.
我试图搜索已经回答但不知道要搜索什么的类似问题.
I tried to search for similar questions that have already been answered but have no idea what to search for.
谢谢您,春节快乐!
推荐答案
这是基本索引和高级索引混合的一种情况.第一个和最后一个索引是数字,中间是切片.它根据 0
和 arange(5)
选择值,并在末尾附加:
维度.
This is a case of mixed basic and advanced indexing. The 1st and last indexes are numeric, and the middle a slice. It selects values based the 0
and arange(5)
, and appends the :
dimension at the end.
aa[0, :, :5].shape
应该产生您期望的(3,5).
should produce the (3,5) you expect.
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#combining-advanced-and-basic-indexing
y = x[0, :, mask]
z = x[0, :, :][:, mask]
请务必检查对我的回答的注释,以证明这是一个错误,并且将得到解决.
Be sure to check the comments to my answer, for the argument that this is a bug and will be fixed.
这篇关于numpy数组索引中的隐式转置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!