I would like to evaluate
x=np.array([1,2,3])
imax=np.where(x==max(x))
Clearly imax=2
but that is not what np.where
returns. To obtain imax=2
, I must use a two-step process that involves doubly dereference the np.where object:
np_where_object=np.where(x==max(x))
imax = np_where_object[0][0]
But this is inelegant. Is there a best practice that avoids the double dereferencing and obtains imax=2
in one pass? And I don't mean np.where(x==max(x))[0][0]
, which is still inelegant.
CodePudding user response:
Yes, there is. It's argmax
:
>>> x = np.array([1,2,3])
>>> imax = x.argmax()
>>> imax
2