Skip to content
Advertisement

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

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

output for b:

JavaScript

output for a[b]:

JavaScript

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement