本文翻译自:How do I get indices of N maximum values in a NumPy array?

NumPy proposes a way to get the index of the maximum value of an array via np.argmax . NumPy提出了一种通过np.argmax数组最大值的索引的np.argmax

I would like a similar thing, but returning the indexes of the N maximum values. 我想要类似的事情,但是返回N最大值的索引。

For instance, if I have an array, [1, 3, 2, 4, 5] , function(array, n=3) would return the indices [4, 3, 1] which correspond to the elements [5, 4, 3] . 例如,如果我有一个数组[1, 3, 2, 4, 5] ,则function(array, n=3)将返回对应于元素[5, 4, 3]的索引[4, 3, 1] 4,3,1 [4, 3, 1] [5, 4, 3]


#1楼

参考:https://stackoom.com/question/Szlx/如何获取NumPy数组中N个最大值的索引


#2楼

Use: 采用:

>>> import heapq
>>> import numpy
>>> a = numpy.array([1, 3, 2, 4, 5])
>>> heapq.nlargest(3, range(len(a)), a.take)
[4, 3, 1]

For regular Python lists: 对于常规的Python列表:

>>> a = [1, 3, 2, 4, 5]
>>> heapq.nlargest(3, range(len(a)), a.__getitem__)
[4, 3, 1]

If you use Python 2, use xrange instead of range . 如果使用Python 2,请使用xrange而不是range

Source: heapq — Heap queue algorithm 来源: heapq —堆队列算法


#3楼

Newer NumPy versions (1.8 and up) have a function called argpartition for this. 较新的NumPy版本(1.8及更高版本)具有一个称为argpartition的函数。 To get the indices of the four largest elements, do 要获取四个最大元素的索引,请执行

>>> a = np.array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])
>>> a
array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])
>>> ind = np.argpartition(a, -4)[-4:]
>>> ind
array([1, 5, 8, 0])
>>> a[ind]
array([4, 9, 6, 9])

Unlike argsort , this function runs in linear time in the worst case, but the returned indices are not sorted, as can be seen from the result of evaluating a[ind] . argsort不同,此函数在最坏的情况下以线性时间运行,但是返回的索引未排序,从评估a[ind]的结果可以看出。 If you need that too, sort them afterwards: 如果您也需要它,请对它们进行排序:

>>> ind[np.argsort(a[ind])]
array([1, 8, 5, 0])

To get the top- k elements in sorted order in this way takes O( n + k log k ) time. 要以这种方式获得排序前k个元素,需要O( n + k log k )时间。


#4楼

Simpler yet: 更简单了:

idx = (-arr).argsort()[:n]

where n is the number of maximum values. 其中, n是最大值的数量。


#5楼

Use: 采用:

from operator import itemgetter
from heapq import nlargest
result = nlargest(N, enumerate(your_list), itemgetter(1))

Now the result list would contain N tuples ( index , value ) where value is maximized. 现在result列表将包含N个元组( indexvalue ),其中value最大化。


#6楼

If you don't care about the order of the K-th largest elements you can use argpartition , which should perform better than a full sort through argsort . 如果您不关心第K个最大元素的顺序 ,则可以使用argpartition ,它的性能要比通过argsort进行完整排序argsort

K = 4 # We want the indices of the four largest values
a = np.array([0, 8, 0, 4, 5, 8, 8, 0, 4, 2])
np.argpartition(a,-K)[-K:]
array([4, 1, 5, 6])

Credits go to this question . 学分到这个问题 。

I ran a few tests and it looks like argpartition outperforms argsort as the size of the array and the value of K increase. 我进行了一些测试,随着数组的大小和K值的增加, argpartition表现优于argsort

如何获取NumPy数组中N个最大值的索引?相关推荐

  1. Python获取numpy数组中最大的5个元素(保持原顺序)

    本文主要演示numpy的argsort()函数的用法.这个函数的返回值是数组中的元素排序后的原下标,例如np.argsort([3,1,2])的返回结果是array([1, 2, 0], dtype= ...

  2. python数组中最大元素_Python获取numpy数组中最大的5个元素(保持原顺序)

    本文主要演示numpy的argsort()函数的用法.这个函数的返回值是数组中的元素排序后的原下标,例如np.argsort([3,1,2])的返回结果是array([1, 2, 0], dtype= ...

  3. numpy使用np.argmax函数获取一维数组中最大值所在的索引(index of largest value in numpy array with np.argmax)

    numpy使用np.argmax函数获取一维数组中最大值所在的索引(index of largest value in numpy array with np.argmax) 目录 numpy使用np ...

  4. python使用np.argsort对一维numpy概率值数据排序获取倒序索引、获取的top索引(例如top2、top5、top10)索引二维numpy数组中对应的原始数据:原始数据概率最大的头部数据

    python使用np.argsort对一维numpy概率值数据排序获取倒序索引.获取的top索引(例如top2.top5.top10)索引二维numpy数组中对应的原始数据:原始数据概率最大的头部数据 ...

  5. python使用np.argsort对一维numpy概率值数据排序获取升序索引、获取的top索引(例如top2、top5、top10)索引二维numpy数组中对应的原始数据:原始数据概率最小的头部数据

    python使用np.argsort对一维numpy概率值数据排序获取升序索引.获取的top索引(例如top2.top5.top10)索引二维numpy数组中对应的原始数据:原始数据概率最小的头部数据 ...

  6. 1646. 获取生成数组中的最大值

    1646. 获取生成数组中的最大值 给你一个整数 n .按下述规则生成一个长度为 n + 1 的数组 nums : nums[0] = 0 nums[1] = 1 当 2 <= 2 * i &l ...

  7. LeetCode 1646. 获取生成数组中的最大值

    1646. 获取生成数组中的最大值 题目:给你一个整数 n .按下述规则生成一个长度为 n + 1 的数组 nums : nums[0] = 0 nums[1] = 1 当 2 <= 2 * i ...

  8. c语言指针生成numpy数组,利用ctypes获取numpy数组的指针方法

    利用ctypes获取numpy数组的指针方法 如下所示: import numpy as np from ctypes import * a = np.asarray(range(16), dtype ...

  9. python取出数组大于某值_Python替换NumPy数组中大于某个值的所有元素实例

    我有一个2D(二维) NumPy数组,并希望用255.0替换大于或等于阈值T的所有值.据我所知,最基础的方法是: shape = arr.shape result = np.zeros(shape) ...

最新文章

  1. java web服务 分层,java web分层之——三层架构
  2. Linux CentOS 5.5 服务器安装图文教程
  3. python实例 87,88
  4. 微软鼠标测试软件,微软发了一款“精准”鼠标 我们告诉你精准在哪儿?
  5. C语言求100以内素数
  6. Ubuntu20.04 Clion/Pycharm/IDEA 输入中文+光标跟随解决方案
  7. 关于linux系统安装zabbix报错的解决方案
  8. MT7688: Shine定点MP3压缩库试验
  9. Spring中Resource接口详解
  10. 64位win7下Android SDK Manager闪退的解决方法
  11. 5G时代的到来会对UI交互设计产生什么影响?
  12. Low rank 论文
  13. matlab动态图阿基米德螺旋,魔兽世界M基尔加丹阿基米德螺旋线之Matlab动图!国服加油...
  14. 【头歌】共享单车之数据存储
  15. 六种黑客入侵手机的常见方式
  16. 斗鱼弹幕服务器第三方接入协议,GitHub - qianjiachun/douyu-point: 为你的斗鱼直播间增加积分功能吧!...
  17. 最简单的TAR,TPR,FAR,FPR的说明
  18. sketch的切图技巧总结:如何批量切出固定大小的切图并保留透明部分?
  19. limma包进行差异分析
  20. win10安装CUDA9.2过程记录

热门文章

  1. android 屏幕录像
  2. C++模板之核心:typename
  3. 算法---字符串去重
  4. 业绩-----我觉得最难得不是写代码,而是写业绩表
  5. Android 判断屏幕方向一个大坑
  6. android的dmtracedump工具生成trace文件图片 'dot' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
  7. python读取xml数据并显示为表格_用Python解析XML数据,然后用SQL创建一个数据库
  8. echart旭日图_150Echarts - 旭日图(Sunburst Label Rotate)
  9. python字典增加和删除_Python字典的基本用法实例分析【创建、增加、获取、修改、删除】...
  10. 华人Hbuy隐私政策