为什么维度的顺序与索引布尔改变? [英] Why does the order of dimensions change with boolean indexing?
问题描述
当我们有 M
形状(A,B,C)
,和一个索引数组 v
,我们使用索引最后一个数组,为什么 M [我,:,v]
导致形状的数组(D,b)
> D 真值的诉数
)?如下图所示:
在[409]:M =零((100,20,40))在[410]:VAL =那些(形状=(40),DTYPE =布尔)在[411]:M [0:,:]形状
出[411]:(20,40)#正如预期的那样在[412]:M [0:,VAL] .shape
出[412]:(40,20)#咦?为什么(40,20),而不是(20,40)?在[413]:M [:,:,VAL] .shape
出[413]:(100,20,40)#s键一下预期
为什么 M [0:,VAL]
具有形状(40,20)
,而不是(20,40)
?
据该文档的布尔索引
部分
的http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean-array-indexing
合并多个布尔索引数组或布尔一个整数数组的索引可以最好地与obj.nonzero()比喻的理解。
块引用>IND = np.nonzero(VAL)[0]
#阵列([0,1,2,...,39],DTYPE = INT32)
M [0:,IND] .shape#(40,20)所以,现在,我们去了一节关于结合先进的和基本的索引
<一href=\"http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#combining-advanced-and-basic-indexing\" rel=\"nofollow\">http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#combining-advanced-and-basic-indexing这是形式的情况下:
X [ARR1,:,ARR2]
在第一种情况下,从高级索引操作产生的尺寸来先结果阵列中,之后,在子空间尺寸。
块引用>所以
0
和IND
部分产生(40)
的选择,而:
中间产生了(20日)
。通过将:最后一个
部分,由此产生的尺寸为(40,20)
。基本上,它这样做是因为有这索引风格混淆,所以一直选择只把部分切片在最后。选择这些值,并保持你想要的形状的方法(或多或少)是使用
np.ix _
来生成索引的元组。M [np.ix _([0],np.arange(20),IND)。形状#(1,20,40)
您可以使用
np.squeeze
来删除初始1
尺寸这使用
第九_
是在纯粹的指数数组索引节的末尾说明
的http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexingWhen we have
M
of shape(a, b, c)
, and an indexing arrayv
that we use to index the last array, why doesM[i, :, v]
result in an array of shape(d, b)
(withd
the number of true values inv
)? As illustrated below:In [409]: M = zeros((100, 20, 40)) In [410]: val = ones(shape=(40,), dtype="bool") In [411]: M[0, :, :].shape Out[411]: (20, 40) # As expected In [412]: M[0, :, val].shape Out[412]: (40, 20) # Huh? Why (40, 20), not (20, 40)? In [413]: M[:, :, val].shape Out[413]: (100, 20, 40) # s expected again
Why does
M[0, :, val]
have shape(40, 20)
rather than(20, 40)
?解决方案According the
boolean indexing
section of the docs http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean-array-indexingCombining multiple Boolean indexing arrays or a Boolean with an integer indexing array can best be understood with the obj.nonzero() analogy.
ind = np.nonzero(val)[0] # array([ 0, 1, 2, ...., 39], dtype=int32) M[0, :, ind].shape # (40,20)
So now we go to the section about combining advanced and basic indexing http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#combining-advanced-and-basic-indexing
This is a case of the form:
x[arr1, :, arr2]
in the first case, the dimensions resulting from the advanced indexing operation come first in the result array, and the subspace dimensions after that.
So the
0
andind
part produce a(40,)
selection, while the:
in the middle produces a(20,)
. By putting the:
part last, the resulting dimension is(40,20)
. Basically it does this because there is an ambiguity in this indexing style, so it consistently opts to put the slice part at the end.A way of selecting these values and maintain the shape you want (more or less) is to use
np.ix_
to generate a tuple of indices.M[np.ix_([0],np.arange(20),ind)].shape # (1, 20, 40)
You can use
np.squeeze
to remove the initial1
dimension.This use of
ix_
is illustrated at the end of the 'purely index array indexing' section http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing这篇关于为什么维度的顺序与索引布尔改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!