为什么维度的顺序与索引布尔改变? [英] Why does the order of dimensions change with boolean indexing?

查看:129
本文介绍了为什么维度的顺序与索引布尔改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们有 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-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)? 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-indexing

Combining 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 and ind 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 initial 1 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆