python - 如何让 numpy.argmax 返回所有出现的最大值?

标签 python numpy max

我正在尝试找到一个函数,该函数返回给定列表中所有次出现的最大值。

numpy.argmax然而只返回它找到的第一个匹配项。例如:

from numpy import argmax

list = [7, 6, 5, 7, 6, 7, 6, 6, 6, 4, 5, 6]
winner = argmax(list)

print winner

只给出索引 0。但我希望它给出所有索引:0, 3, 5.

最佳答案

正如 np.argmax 的文档所说:“如果多次出现最大值,则返回与第一次出现相对应的索引。”,所以你将需要另一种策略。

你有一个选择是使用 np.argwhere 结合 np.amax:

>>> import numpy as np
>>> listy = [7, 6, 5, 7, 6, 7, 6, 6, 6, 4, 5, 6]
>>> winner = np.argwhere(listy == np.amax(listy))
>>> print(winner)
 [[0]
  [3]
  [5]]
>>> print(winner.flatten().tolist()) # if you want it as a list
[0, 3, 5]

关于python - 如何让 numpy.argmax 返回所有出现的最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17568612/

相关文章:

python - 根据列的最大值删除 Pandas 数据框行

sql - 多对多关系中的最大值

java - 再现行为 MAX_VALUE 和 MIN_VALUE

python - python函数的可选参数字符串和数据框

python - 替换两个子字符串之间的单词(保留其他单词)

python - 使用 WSGI 和 Python 3 提供静态文件

php - 如何返回给定产品的最高分图像?

python - 将 Python (numpy) 中的小图像精确调整为原始大小的倍数

python - Numpy:扁平化字典

python - 整数数组的基本转换