简体   繁体   English

为什么使用布尔索引会改变尺寸顺序?

[英]Why does the order of dimensions change with boolean indexing?

When we have M of shape (a, b, c) , and an indexing array v that we use to index the last array, why does M[i, :, v] result in an array of shape (d, b) (with d the number of true values in v )? 当我们有形状为(a, b, c) M和用于索引最后一个数组的索引数组v ,为什么M[i, :, v]导致形状为(d, b)的数组(与dv的真值个数? 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) ? 为什么M[0, :, val]具有形状(40, 20) M[0, :, val] (40, 20)而不是(20, 40)

According the boolean indexing section of the docs http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean-array-indexing 根据docs http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean-array-indexingboolean indexing部分

Combining multiple Boolean indexing arrays or a Boolean with an integer indexing array can best be understood with the obj.nonzero() analogy. 可以通过obj.nonzero()类比来最好地理解将多个布尔索引数组或布尔与整数索引数组组合在一起的方法。

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 因此,现在我们转到有关结合高级索引和基本索引的部分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] 这是以下形式的情况: 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 and ind part produce a (40,) selection, while the : in the middle produces a (20,) . 所以0ind部分产生(40,)选择,而中间的:产生(20,) By putting the : part last, the resulting dimension is (40,20) . 通过将:部分放在最后,得出的尺寸为(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. 选择这些值并保持所需形状(或多或少)的一种方法是使用np.ix_生成索引元组。

M[np.ix_([0],np.arange(20),ind)].shape # (1, 20, 40)

You can use np.squeeze to remove the initial 1 dimension. 您可以使用np.squeeze删除初始的1维。

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 这种使用的ix_在“纯粹索引数组索引”部分的端部被示出http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing

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

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