Reputation: 53
As an input i have 2d numpy array, lets suppose this one:
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:
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.
I've tried using np.where
and np.sum
, ended up with this:
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?
Upvotes: 1
Views: 63
Reputation: 231355
The problem with your where
expression occurs inside it, in your attempt to combine two conditions:
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
is a scalar operation, and does not play well with boolean arrays.
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
:
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]))
Upvotes: 2
Reputation: 7510
Here is a solution using product from itertools. Creating a list of rows and columns with sums == 0 and finding the combinations between them.
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)))
Upvotes: 0