简体   繁体   English

为什么二维 NumPy 数组的布尔索引会产生一维数组?

[英]Why does boolean indexing of a 2D NumPy array produces 1d array?

I was experimenting with boolean indexing in NumPy and came across this which is confusing me:我在 NumPy 中尝试使用布尔索引并遇到了这个让我感到困惑的问题:

import numpy as np

np.random.seed(0)

Created a 7 x 4 array:创建了一个 7 x 4 数组:

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]]

Created a boolean array also 7 x 4:也创建了一个 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]

Output:输出:

[ 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]

How can this be explained?这怎么解释? I reasoned it as follows: the number of rows is the same (ie 7).我的推理如下:行数相同(即7)。 For each row, True is found at positions 0 and 3 (ie 2 values).对于每一行,在位置 0 和 3(即 2 个值)处发现True Thus I end up getting a 1 x 14 matrix.因此我最终得到一个 1 x 14 的矩阵。 I was expecting a 7 x 2 matrix though.不过,我期待一个 7 x 2 矩阵。

Could someone please clarify how this is evaluated to give a 1 x 14 matrix?有人可以澄清如何评估它以给出 1 x 14 矩阵吗?

Numpy has no a-priori way of knowing where the True elements of your mask will be. Numpy 没有先验方式知道你的面具的True元素在哪里。 It is purely happenstance that your selection is aligned so neatly in columns.您的选择在列中如此整齐地对齐纯粹是偶然的。

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. Numpy 必须能够一致地处理所有这些情况。 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]]

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

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