开发者

Numpy: how to retrieve k max values from each row?

开发者 https://www.devze.com 2023-01-11 16:12 出处:网络
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).Do

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

精彩评论

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