Reputation: 209
I have a numpy.array
of data called f, I know the max value in it is f_max=max(f)
but I would like to know the index in the array corresponding to the maximum value.
I tried:
count = 0
while (f[count]!=fmax)
conto ++
but I receive an error:
SyntaxError: invalid syntax
Could anyone help me?
Upvotes: 0
Views: 5297
Reputation: 406
If you're already using numpy, you can do this with argmax().
import numpy as np
a = np.array([1, 5, 2, 6, 3])
index = a.argmax()
Upvotes: 4
Reputation: 34531
The easiest way would be to find the max
and then, look for its index.
>>> a = [1, 5, 2, 3, 4]
>>> val = max(a)
>>> a.index(val)
1
You could also use enumerate
to get a list of indices and the values corresponding to them and choose the max among them.
>>> list(enumerate(a))
[(0, 1), (1, 5), (2, 2), (3, 3), (4, 4)]
>>> index, _ = max(enumerate(a), key = lambda x: x[1])
>>> index
1
Upvotes: 1