tags:

views:

13

answers:

1

Hi,
I want to retrieve k max values from each row in a numpy array. I have been digging through the documentation but couldn't find an answer (I am probably looking in the wrong places). Does anybody have a simple code snippet that does this?

thanks so much,

Diederik

+1  A: 

Here's a simple solution:

some_array = numpy.random.randint(0, 10, 10)
top_items = numpy.sort(some_array)[-k:]

Or as a lambda:

max_values = lambda k, array: numpy.sort(array)[-k:]
WoLpH