I understand the output of np.where()
with input of a one-row array. However, when a two-row array was used as an input, I don’t understand why the output of b is two arrays.
The output for a[b] makes sense.
JavaScript
x
7
1
a = np.array([[1, 2, 3],[4,5,6]])
2
print(a)
3
print ('Indices of elements <4')
4
b = np.where(a<4)
5
print(b)
6
print(a[b])
7
output for b:
JavaScript
1
2
1
(array([0, 0, 0], dtype=int64), array([0, 1, 2], dtype=int64))
2
output for a[b]:
JavaScript
1
2
1
[1 2 3]
2
Advertisement
Answer
We require two indices to access each element in 2D array. For eg. i and j.
Hence, if the indices of the 2D array satisfying the condition are (i1,j1), (i2,j2) and (i3,j3)
for condition a<4
, then np.where()
will return a tuple of tuples in format like ((i1,i2,i3),(j1,j2,j3))