Python 在numpy数组中查找最大值及其索引

Python 在numpy数组中查找最大值及其索引,python,opencv,Python,Opencv,我有这样一个数组: [ [0.0], [0.0020777732133865356], [0.0013433878775686026], [0.00021494206157512963], [8.955918747233227e-05], [0.0], [0.0], [1.7911837858264334e-05], [0.0], [1.7911837858264334e-05], [0.0], [0.0], [0.0], [0.0007702090078964829], [0.02625875

我有这样一个数组:

[ [0.0], [0.0020777732133865356], [0.0013433878775686026], [0.00021494206157512963], [8.955918747233227e-05], [0.0], [0.0], [1.7911837858264334e-05], [0.0], [1.7911837858264334e-05], [0.0], [0.0], [0.0], [0.0007702090078964829], [0.02625875361263752], [0.13904960453510284], [0.30124127864837646], [0.30514606833457947], [0.4224506914615631], [0.45712801814079285], [0.5807734131813049], [0.5874545574188232], [0.695248007774353], [0.18126779794692993], [0.11689265072345734], [0.07207723706960678], [0.06512743979692459], [0.06016586348414421], [0.04363323748111725], [0.030235182493925095], [0.03095165640115738], [0.028963441029191017], [0.03578785061836243], [0.029267942532896996]]
我想找到最大值及其索引。 我已经搜索过了,但没有一个符合我的问题。 有什么解决办法吗? 谢谢。

你怎么了

In [6]: ind = np.argmax(a)

In [7]: a[ind]
Out[7]: array([ 0.69524801])
由于您有一个二维数组,您可能更喜欢:

In [9]: a[ind][0]
Out[9]: 0.69524800777435303

如果可以有多个最大值:

In [16]: arr = np.array([1,4,3,2,5,6,3,5,7,4,7,1,4,7,3])
In [17]: np.max(arr)
Out[17]: 7
In [18]: np.where(arr == np.max(arr))
Out[18]: (array([ 8, 10, 13]),)

因此,您有一个嵌套列表。下面是步骤

创建一个新列表 将所有元素添加到此列表中 对列表进行排序,得到最大值并得到索引

这是你的数组

myList = [ ]
for mylist in a:  
    print mylist[0]
    myList.append(mylist[0])
复制列表
看看怎么样?您不需要对整个列表进行排序就可以得到max元素。这是唯一的复杂性,而不是唯一的复杂性。此外,我不知道如何使用这种方法获取索引,除非将索引与值一起存储,然后获取该元组的最大值。我这样做了,但错误是:AttributeError:'numpy.ndarray'对象没有属性'append',我还想用各自的索引提取5个最大值。@user5538206当您说5个最大值时,你是指5个最高值,还是5个恰好是同一个最大值的值?我是指5个最高值:
import copy
sortedList = copy.copy(myList)
sortedList.sort()
sortedList.reverse()
# to Get the 5 maximum values from the list
for i in range(0,4):
    print sortedList[i]
    print myList.index(sortedList[i]