Since arr is 2d, iteration gives you the rows, not the elements.
Make a sample array:
In [347]: arr=np.arange(16).reshape(4,4)-10
In [348]: arr
Out[348]:
array([[-10, -9, -8, -7],
[ -6, -5, -4, -3],
[ -2, -1, 0, 1],
[ 2, 3, 4, 5]])
Iterate with some prints:
In [350]: for i in arr:
print(i)
print(i<0)
print((i<0).any())
.....:
result:
[-10 -9 -8 -7]
[ True True True True]
True
[-6 -5 -4 -3]
[ True True True True]
True
[-2 -1 0 1]
[ True True False False]
True
[2 3 4 5]
[False False False False]
False
The ValueError results when you try to use that boolean array np.array([True, True, False, False]) in an if statement. Applying all or any to the array reduces it to one scalar True/False value, which works in the if statement.
You can apply the negative test to the whole array, and apply all/any to rows (or columns) – without iteration:
In [351]: arr<0
Out[351]:
array([[ True, True, True, True],
[ True, True, True, True],
[ True, True, False, False],
[False, False, False, False]], dtype=bool)
In [352]: (arr<0).any(axis=1)
Out[352]: array([ True, True, True, False], dtype=bool)
In [353]: (arr<0).all(axis=1)
Out[353]: array([ True, True, False, False], dtype=bool)
Подробнее ...