如何在带有索引的2D数组中查找最大值 [英] How to find max value in 2D array with index

查看:67
本文介绍了如何在带有索引的2D数组中查找最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一种优雅的方法,如何在带有python中索引的2D数组中找到最大值。我用

I want to find some elegant way, how to find max value in 2D array with indexes in python. I used

np.amax(array)

用于搜索最大值,但是我不知道如何获取索引。当然,我可以通过for循环找到它,但是我认为有更好的方法。有人可以帮我吗?

for searching max value, but I don't know how to get indexes. Of course, I could find it by for loops, but I think there is some better way. Could someone help me? Thank you in advance.

推荐答案

请参阅此,其中还详细说明了如何找到最大值及其索引,您可以使用 argmax()

>>> a = array([[10,50,30],[60,20,40]])
>>> maxindex = a.argmax()
>>> maxindex
3

您可以使用 unravel_index(a.argmax( ),a.shape)以将索引作为元组获取:

You can use unravel_index(a.argmax(), a.shape) to get the index as a tuple:

>>> from numpy import unravel_index
>>> unravel_index(a.argmax(), a.shape)
(1, 0)

这篇关于如何在带有索引的2D数组中查找最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆