[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()"""
    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"""
    for index, item in enumerate(myList):
        if item[0]>maxVal:
    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


Howard Chong
Dept. of Agricultural and Resource Economics and Energy Institute @ Haas
Business School
UC Berkeley
Cell: 510-333-0539
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20091202/1d121a34/attachment-0001.html 

More information about the NumPy-Discussion mailing list