# [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

Howard Chong hgchong@berkeley....
Wed Dec 2 17:55:00 CST 2009

```I will need to find the N largest numbers and corresponding indexes in an
1-D array.

If N==1, I can easily do:

def myFindMaxC(myList):
"""implement finding maximum value with using numpy.array()"""
myA=np.array(myList)
maxIndex=myA.argmax()
maxVal=myA[maxIndex]
return [maxIndex, maxVal]

For me, I'm likely going to be running this with N==7.

So, I think I have to iterate over the array. Doing it with non-numpy
procedures is **quite slow**. Here, I run it with N==1 without using any
numpy procedures.

def myFindMaxA(myList):
"""implement finding maximum value with for loop iteration"""
maxIndex=0
maxVal=myList[0]
for index, item in enumerate(myList):
if item[0]>maxVal:
maxVal=item[0]
maxIndex=index
return [maxIndex, maxVal]

My question is: how can I make the latter version run faster? I think the
answer is that I have to do the iteration in C.

If that's the case, can anyone point me to where np.array.argmax() is
implemented so I can write np.array.argmaxN() extend it to the N largest
values?

Thanks!

--
Howard Chong
Dept. of Agricultural and Resource Economics and Energy Institute @ Haas