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(a)
print ('Indices of elements <4')
b = np.where(a<4)
print(b)
print(a[b])
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.