Numpy: how to retrieve k max values from each row?
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
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:]
精彩评论