为什么二维NumPy数组的布尔索引会生成一维数组? [英] Why does boolean indexing of a 2D NumPy array produces 1d array?

查看:246
本文介绍了为什么二维NumPy数组的布尔索引会生成一维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在NumPy中进行布尔索引的实验,碰到了这一点,这使我感到困惑:

I was experimenting with boolean indexing in NumPy and came across this which is confusing me:

import numpy as np

np.random.seed(0)

创建了一个7 x 4的阵列:

Created a 7 x 4 array:

data = np.random.rand(7, 4) 

[[ 0.5488  0.7152  0.6028  0.5449]

 [ 0.4237  0.6459  0.4376  0.8918]

 [ 0.9637  0.3834  0.7917  0.5289]

 [ 0.568   0.9256  0.071   0.0871]

 [ 0.0202  0.8326  0.7782  0.87  ]

 [ 0.9786  0.7992  0.4615  0.7805]

 [ 0.1183  0.6399  0.1434  0.9447]]

还创建了一个7 x 4的布尔数组:

Created a boolean array also 7 x 4:

bool_array = 

         ([[True,False,False,True],
          [True,False,False,True],
          [True,False,False,True],
          [True,False,False,True],
          [True,False,False,True],
          [True,False,False,True],
          [True,False,False,True]])


bool_array = np.array(bool_array)

data[bool_array]

输出:

[ 0.5488  0.5449  0.4237  0.8918  0.9637  0.5289  0.568   0.0871  0.0202
  0.87    0.9786  0.7805  0.1183  0.9447]

这怎么解释?我的理由如下:行数相同(即7).对于每一行,在位置0和3(即2个值)处找到True.因此,我最终得到了一个1 x 14的矩阵.我期待的是7 x 2矩阵.

How can this be explained? I reasoned it as follows: the number of rows is the same (i.e. 7). For each row, True is found at positions 0 and 3 (i.e. 2 values). Thus I end up getting a 1 x 14 matrix. I was expecting a 7 x 2 matrix though.

有人可以澄清一下如何评估得出1 x 14的矩阵吗?

Could someone please clarify how this is evaluated to give a 1 x 14 matrix?

推荐答案

Numpy无法先验地知道蒙版的True元素在哪里.完全偶然的是,您的选择在列中如此整齐地对齐.

Numpy has no a-priori way of knowing where the True elements of your mask will be. It is purely happenstance that your selection is aligned so neatly in columns.

要了解为什么将结果放入一个1D数组中,请想象如何处理在每一行中都有两个选择,但并不总是来自同一列的情况.现在,设想一种情况,每行中的选择数量不同,可能有些行完全为空. Numpy必须能够始终如一地处理所有这些情况.这样做会很慢,并且会导致很多问题,具体取决于蒙版的内容而返回不同形状的数组.

To understand why the result is raveled into a 1D array, imagine how to handle the case where you have two selections in each row, but not always from the same column. Now imagine a case where the number of selections in each row is different, possibly with some rows completely empty. Numpy has to be able to handle all these cases consistently. It would be much slower and would cause a lot of problems to return an array of different shape depending on the contents of your mask.

要选择所需的列,请使用适当的索引:

To make the selection of the columns you want, use the appropriate index:

a[:, ::3]

OR

a[:, [0, 3]]

这篇关于为什么二维NumPy数组的布尔索引会生成一维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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