开发者

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:]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜