[英]Finding index of the element in a numpy array witout using for loop
As an input i have 2d numpy array, lets suppose this one: 作为输入,我有2d numpy数组,让我们假设这个:
my_array = np.matrix([[3, 7, 0, 0],
[0, 2, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0]])
I have to find the index of every element in that array in which the sum of elements in that row and column == 0. In this case, the answer would be (2, 3), since the sum of elements in second row = 0 and sum of elements in 3rd column = 0. So far i came up with this: 我必须找到该行和列中的元素之和== 0的那个数组中每个元素的索引。在这种情况下,答案将是(2,3),因为第二行中的元素之和= 0,第3列中的元素总和=0。到目前为止,我想到了这个:
solution = [(i, j) for i in range(my_array.shape[0]) for j in range(my_array.shape[1]) if 1 not in my_array[i] and 1 not in my_array[:, j]]
The problem is, I want to do this without using for loop. 问题是,我想这样做而不使用for循环。
I've tried using np.where
and np.sum
, ended up with this: 我试过使用np.where
和np.sum
,结果是这样:
np.where(np.sum(my_array, axis=1) == 0 and np.sum(my_array, axis=0) == 0)
but i end up with this error: 但我最终遇到此错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Any suggestion about how can i fix this error or just use other method to find the indexes? 关于如何解决此错误或仅使用其他方法查找索引的任何建议?
The problem with your where
expression occurs inside it, in your attempt to combine two conditions: 尝试合并两个条件时where
表达式中出现where
的问题:
In [210]: np.sum(arr, axis=1) == 0 and np.sum(arr, axis=0) == 0
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-210-46c837435a31> in <module>
----> 1 np.sum(arr, axis=1) == 0 and np.sum(arr, axis=0) == 0
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [211]: (np.sum(arr, axis=1) == 0) & (np.sum(arr, axis=0) == 0)
Out[211]: array([False, False, False, False])
You have to wrap the ==
test inside () so it occurs first, and you have to use &
to perform element-wise and
. 您必须将==
测试包装在()内,以便它首先出现,并且您必须使用&
进行元素级and
。 and
is a scalar operation, and does not play well with boolean arrays. and
是标量运算,在布尔数组中不能很好地发挥作用。
The row and column tests are: 行和列测试是:
In [212]: arr.sum(0)==0
Out[212]: array([False, False, False, True])
In [213]: arr.sum(1)==0
Out[213]: array([False, False, True, False])
but you want a kind of outer or cartesian combination, not a simple element-wise combination (that would be more obvious if there were different numbers of rows and columns). 但是您需要一种外部或笛卡尔组合,而不是简单的按元素组合(如果行和列的数量不同,这将更加明显)。
In [218]: (arr.sum(1)==0)[:,None] & (arr.sum(0)==0)
Out[218]:
array([[False, False, False, False],
[False, False, False, False],
[False, False, False, True],
[False, False, False, False]])
In [219]: np.where(_)
Out[219]: (array([2]), array([3]))
Or with the keepdims
parameter of sum
: 或使用sum
的keepdims
参数:
In [220]: arr.sum(0, keepdims=True)==0
Out[220]: array([[False, False, False, True]])
In [221]: arr.sum(1, keepdims=True)==0
Out[221]:
array([[False],
[False],
[ True],
[False]])
In [222]: np.where(_220 & _221) # Out[220] etc
Out[222]: (array([2]), array([3]))
Here is a solution using product from itertools. 这是使用itertools产品的解决方案。 Creating a list of rows and columns with sums == 0 and finding the combinations between them. 创建总和== 0的行和列的列表,并找到它们之间的组合。
from itertools import product
my_array = np.matrix([[3, 7, 0, 0],
[0, 2, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0]])
a = np.argwhere(my_array.sum(axis = 1) == 0)[:,0]
b = np.argwhere(my_array.sum(axis = 0) == 0)[:,1]
np.array(list(product(a,b)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.