Python 通过布尔掩码数组选择numpy数组的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19984102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:08:46  来源:igfitidea点击:

Select elements of numpy array via boolean mask array

pythonnumpyindexingbooleanmask

提问by JohnDoe

I have a boolean mask array aof length n:

我有一个a长度为布尔值的掩码数组n

a = np.array([True, True, True, False, False])

I have a 2d array with ncolumns:

我有一个带n列的二维数组:

b = np.array([[1,2,3,4,5], [1,2,3,4,5]])

I want a new array which contains only the "True"-values, for example

例如,我想要一个仅包含“真”值的新数组

c = ([[1,2,3], [1,2,3]])

c = a * bdoes not work because it contains also "0" for the false columns what I don't want

c = a * b不起作用,因为它也包含我不想要的假列的“0”

c = np.delete(b, a, 1) does not work

Any suggestions?

有什么建议?

回答by DSM

You probably want something like this:

你可能想要这样的东西:

>>> a = np.array([True, True, True, False, False])
>>> b = np.array([[1,2,3,4,5], [1,2,3,4,5]])
>>> b[:,a]
array([[1, 2, 3],
       [1, 2, 3]])

Note that for this kind of indexing to work, it needs to be an ndarray, like you were using, not a list, or it'll interpret the Falseand Trueas 0and 1and give you those columns:

请注意,要使这种索引工作,它需要是ndarray,就像您使用的那样,而不是 a list,否则它会将Falseand解释True0and1并为您提供这些列:

>>> b[:,[True, True, True, False, False]]   
array([[2, 2, 2, 1, 1],
       [2, 2, 2, 1, 1]])

回答by Magi

You can use numpy.mamodule and use np.ma.masked_arrayfunction to do so.

您可以使用numpy.mamodule 和 use np.ma.masked_arrayfunction 来做到这一点。

>>> x = np.array([1, 2, 3, -1, 5])                                                
>>> mx = ma.masked_array(x, mask=[0, 0, 0, 1, 0])
masked_array(data=[1, 2, 3, --, 5], mask=[False, False,  False, True, False], fill_value=999999)

回答by Raimi bin Karim

Hope I'm not too late! Here's your array:

希望我还不算太晚!这是你的数组:

X = np.array([[1, 2, 3, 4, 5], 
              [1, 2, 3, 4, 5]])

Let's create an array of zeros of the same shape as X:

让我们创建一个形状相同的零数组X

mask = np.zeros_like(X)
# array([[0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0]])

Then, specify the columns that you want to mask out or hide with a 1. In this case, we want the last 2 columns to be masked out.

然后,使用1. 在这种情况下,我们希望将最后 2 列屏蔽掉。

mask[:, -2:] = 1
# array([[0, 0, 0, 1, 1],
#        [0, 0, 0, 1, 1]])

Create a masked array:

创建一个屏蔽数组:

X_masked = np.ma.masked_array(X, mask)
# masked_array(data=[[1, 2, 3, --, --],
#                    [1, 2, 3, --, --]],
#              mask=[[False, False, False,  True,  True],
#                    [False, False, False,  True,  True]],
#              fill_value=999999)

We can then do whatever we want with X_masked, like taking the sum of each column (along axis=0):

然后我们可以对 做任何我们想做的事情X_masked,比如计算每列的总和(沿着axis=0):

np.sum(X_masked, axis=0)
# masked_array(data=[2, 4, 6, --, --],
#              mask=[False, False],
#              fill_value=1e+20)

Great thing about this is that X_maskedis just a view of X, not a copy.

很棒的一点是这X_masked只是一个视图X,而不是副本。

X_masked.base is X
# True