如何在整个带有索引的2D数组中找到最大值 [英] How to find maximum value in whole 2D array with indices

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

问题描述

我想使用NumPy在2D数组中找到最大值,并在Python中找到最大值的索引.我用过

I want to find the maximum value in a 2D array and the indices of the maximum value in Python using NumPy. I used

np.amax(array)

用于搜索最大值,但是我不知道如何获取其索引.我可以通过使用"for"循环来找到它,但是也许有更好的方法.

for searching for the maximum value, but I don't know how to get its indices. I could find it by using ´for` loops, but maybe there is a better way.

推荐答案

请参阅此

Refer to this answer, which also elaborates how to find the max value and its (1D) index, you can use argmax()

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

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

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

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

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

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