获取numpy数组中最大项的位置 [英] Get the position of the biggest item in a numpy array

查看:154
本文介绍了获取numpy数组中最大项的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得多维numpy数组中最大项的位置?

How can I get get the position of the biggest item in a multi-dimensional numpy array?

推荐答案

argmax() 方法应该有所帮助。

The argmax() method should help.

更新

(阅读评论后)我相信 argmax()方法也适用于多维数组。链接文档提供了一个示例:

(After reading comment) I believe the argmax() method would work for multi dimensional arrays as well. The linked documentation gives an example of this:

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

更新2

(感谢 KennyTM 的评论)你可以使用 unravel_index(a.argmax(),a.shape)将索引作为元组获取:

(Thanks to KennyTM's comment) 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)

这篇关于获取numpy数组中最大项的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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