Python 在 NumPy 数组中查找等于零的元素的索引

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

Find indices of elements equal to zero in a NumPy array

pythonnumpy

提问by gotgenes

NumPy has the efficient function/method nonzero()to identify the indices of non-zero elements in an ndarrayobject. What is the most efficient way to obtain the indices of the elements that dohave a value of zero?

NumPy 具有高效的函数/方法nonzero()来识别ndarray对象中非零元素的索引。什么是最有效的方式来获得该元素的索引具有零值?

采纳答案by mtrw

numpy.where()is my favorite.

numpy.where()是我的最爱。

>>> x = numpy.array([1,0,2,0,3,0,4,5,6,7,8])
>>> numpy.where(x == 0)[0]
array([1, 3, 5])

回答by nate c

You can search for any scalar condition with:

您可以使用以下命令搜索任何标量条件:

>>> a = np.asarray([0,1,2,3,4])
>>> a == 0 # or whatver
array([ True, False, False, False, False], dtype=bool)

Which will give back the array as an boolean mask of the condition.

这将返回数组作为条件的布尔掩码。

回答by dvdvck

If you are working with a one-dimensional array there is a syntactic sugar:

如果您正在使用一维数组,则有一个语法糖:

>>> x = numpy.array([1,0,2,0,3,0,4,5,6,7,8])
>>> numpy.flatnonzero(x == 0)
array([1, 3, 5])

回答by Dusch

You can also use nonzero()by using it on a boolean mask of the condition, because Falseis also a kind of zero.

您也可以nonzero()在条件的布尔掩码上使用它,因为False它也是一种零。

>>> x = numpy.array([1,0,2,0,3,0,4,5,6,7,8])

>>> x==0
array([False, True, False, True, False, True, False, False, False, False, False], dtype=bool)

>>> numpy.nonzero(x==0)[0]
array([1, 3, 5])

It's doing exactly the same as mtrw's way, but it is more related to the question ;)

它的做法与mtrw's 的方式完全相同,但与问题更相关;)

回答by sramij

import numpy as np

x = np.array([1,0,2,3,6])
non_zero_arr = np.extract(x>0,x)

min_index = np.amin(non_zero_arr)
min_value = np.argmin(non_zero_arr)

回答by MSeifert

There is np.argwhere,

np.argwhere

import numpy as np
arr = np.array([[1,2,3], [0, 1, 0], [7, 0, 2]])
np.argwhere(arr == 0)

which returns all found indices as rows:

它将所有找到的索引作为行返回:

array([[1, 0],    # Indices of the first zero
       [1, 2],    # Indices of the second zero
       [2, 1]],   # Indices of the third zero
      dtype=int64)

回答by Jeril

I would do it the following way:

我会这样做:

>>> x = np.array([[1,0,0], [0,2,0], [1,1,0]])
>>> x
array([[1, 0, 0],
       [0, 2, 0],
       [1, 1, 0]])
>>> np.nonzero(x)
(array([0, 1, 2, 2]), array([0, 1, 0, 1]))

# if you want it in coordinates
>>> x[np.nonzero(x)]
array([1, 2, 1, 1])
>>> np.transpose(np.nonzero(x))
array([[0, 0],
       [1, 1],
       [2, 0],
       [2, 1])

回答by chmnsk

You can use numpy.nonzero to find zero.

您可以使用 numpy.nonzero 来查找零。

>>> import numpy as np
>>> x = np.array([1,0,2,0,3,0,0,4,0,5,0,6]).reshape(4, 3)
>>> np.nonzero(x==0)  # this is what you want
(array([0, 1, 1, 2, 2, 3]), array([1, 0, 2, 0, 2, 1]))
>>> np.nonzero(x)
(array([0, 0, 1, 2, 3, 3]), array([0, 2, 1, 1, 0, 2]))