0

Since argmax only gives one maximum values,how can we find atleast 2 or 3 elements instead of just one.

Currently my input is in the format np.argmax(array,axis=2) which is giving only one maximum and i have to extract 2 or 3 atleast from the array which is N-dimensional

4
  • What do you mean by "2 or 3 elements"? np.argmax extracts the argmax of all arrays in one dimension. Are you looking for the highest, second highest and third highest? Commented Jun 17, 2018 at 10:57
  • Question has nothing to do with machine-learning - kindly do not spam the tag (removed) Commented Jun 17, 2018 at 11:08
  • 2
    Possible duplicate of stackoverflow.com/questions/6910641/… Commented Jun 17, 2018 at 11:25
  • Yeah the 2nd highest,3rd highest and so on Commented Jun 17, 2018 at 11:31

2 Answers 2

1

I would try to use the function called argpartition(). To get the indices of the two largest elements, do:

import numpy as np

a = np.array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])

ind = np.argpartition(a, -2)[-2:] 

ind
Out[13]: array([5, 0], dtype=int64)

a[ind]
Out[14]: array([9, 9])
Sign up to request clarification or add additional context in comments.

Comments

1

Using numpy.argsort. Data from @CarlesSansFuentes.

import numpy as np

a = np.array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])

args = np.argsort(-a)[:2]

array([0, 5], dtype=int64)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.