JustinGong
JustinGong

Reputation: 429

can not understand the result of np.where(array1==array2)

I have a code below:

import numpy as np
a=np.array([[0., 4., 1.0],[2., 3., 2.0]])
argm=a.argmax(axis=0)
m=a[argm,np.arange(3)]
print (np.where(a == m))

I can not understand the result:

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

anyone can explain it? thank you very much!!

Upvotes: 1

Views: 370

Answers (1)

dsm
dsm

Reputation: 2253

Your a and m arrays look like this:

In [2]: a
Out[2]: 
array([[ 0.,  4.,  1.],
       [ 2.,  3.,  2.]])

In [5]: m
Out[5]: array([ 2.,  4.,  2.])

This is the result of a == m:

In [7]: a == m
Out[7]: 
array([[False,  True, False],
       [ True, False,  True]], dtype=bool)

You can think of the result of np.where(a==m) as returning pairs of coordinates: the first True it located is in row 0 (as seen in the first returned array), column 1 (per the second one), as you can verify for yourself. The second one is in row 1, column 0. Likewise for the third one.

Is that clear enough?

You may also want to look at the docs for numpy.nonzero, which is what numpy.where collapses to when given a single argument.

Upvotes: 4

Related Questions