Why does numpy.where() give two arrays with an array of more than 1 row as input?

Tags: , , ,

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.

a = np.array([[1, 2, 3],[4,5,6]])
print ('Indices of elements <4')
b = np.where(a<4)

output for b:

(array([0, 0, 0], dtype=int64), array([0, 1, 2], dtype=int64))

output for a[b]:

[1 2 3]


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))

Source: stackoverflow