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

2024-03-28 22:45:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数组,比如:

[ [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]]

我想找到最大值及其索引。 我已经找过了,但没有一个符合我的问题。 有什么解决办法吗? 谢谢。


Tags: 数组解决办法
3条回答

怎么了

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])

复制列表

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]

相关问题 更多 >