Python 使用numpy查找矩阵中哪些行的所有元素都为零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23726026/
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 03:23:17  来源:igfitidea点击:

Finding which rows have all elements as zeros in a matrix with numpy

pythonnumpymatrix

提问by Hyman Twain

I have a large numpymatrix M. Some of the rows of the matrix have all of their elements as zero and I need to get the indices of those rows. The naive approach I'm considering is to loop through each row in the matrix and then check each elements. However I think there's a better and a faster approach to accomplish this using numpy. I hope you can help!

我有一个大numpy矩阵M。矩阵的某些行的所有元素都为零,我需要获取这些行的索引。我正在考虑的幼稚方法是遍历矩阵中的每一行,然后检查每个元素。但是,我认为使用numpy. 我希望你能帮忙!

采纳答案by Warren Weckesser

Here's one way. I assume numpy has been imported using import numpy as np.

这是一种方法。我假设 numpy 已使用import numpy as np.

In [20]: a
Out[20]: 
array([[0, 1, 0],
       [1, 0, 1],
       [0, 0, 0],
       [1, 1, 0],
       [0, 0, 0]])

In [21]: np.where(~a.any(axis=1))[0]
Out[21]: array([2, 4])

It's a slight variation of this answer: How to check that a matrix contains a zero column?

这是这个答案的轻微变化:如何检查矩阵包含零列?

Here's what's going on:

这是发生了什么:

The anymethod returns True if any value in the array is "truthy". Nonzero numbers are considered True, and 0 is considered False. By using the argument axis=1, the method is applied to each row. For the example a, we have:

any如果数组中的任何值是“真实的”,则该方法返回 True。非零数被认为是 True,0 被认为是 False。通过使用参数axis=1,该方法应用于每一行。例如a,我们有:

In [32]: a.any(axis=1)
Out[32]: array([ True,  True, False,  True, False], dtype=bool)

So each value indicates whether the corresponding row contains a nonzero value. The ~operator is the binary "not" or complement:

所以每个值都表示相应的行是否包含非零值。该~操作是二进制“不是”或补充:

In [33]: ~a.any(axis=1)
Out[33]: array([False, False,  True, False,  True], dtype=bool)

(An alternative expression that gives the same result is (a == 0).all(axis=1).)

(给出相同结果的另一种表达式是(a == 0).all(axis=1)。)

To get the row indices, we use the wherefunction. It returns the indices where its argument is True:

要获取行索引,我们使用该where函数。它返回参数为 True 的索引:

In [34]: np.where(~a.any(axis=1))
Out[34]: (array([2, 4]),)

Note that wherereturned a tuple containing a single array. whereworks for n-dimensional arrays, so it always returns a tuple. We want the single array in that tuple.

请注意,where返回了一个包含单个数组的元组。 where适用于 n 维数组,因此它始终返回一个元组。我们想要该元组中的单个数组。

In [35]: np.where(~a.any(axis=1))[0]
Out[35]: array([2, 4])

回答by crypdick

The accepted answer works if the elements are int(0). If you want to find rows where all the values are 0.0 (floats), you have to use np.isclose():

如果元素是 ,则接受的答案有效int(0)。如果要查找所有值为 0.0(浮点数)的行,则必须使用np.isclose()

print(x)
# output
tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 1., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0.],
])
np.where(np.all(np.isclose(labels, 0), axis=1))
(array([ 0, 3]),)

Note: this also works with PyTorch Tensors, which is nice for when you want to find zeroed multihot encoding vectors.

注意:这也适用于 PyTorch 张量,当您想找到归零的多热编码向量时,这非常有用。