如何获取numpy array前N个最大值


Posted in Python onMay 14, 2021

主要应用了argsort()函数,函数原型:

numpy.argsort(a, axis=-1, kind='quicksort', order=None)
'''
Returns the indices that would sort an array.
Perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in sorted order.
'''
Parameters: 
a : array_like
Array to sort.
 
axis : int or None, optional
Axis along which to sort. The default is -1 (the last axis). If None, the flattened array is used.
 
kind : {‘quicksort', ‘mergesort', ‘heapsort', ‘stable'}, optional
Sorting algorithm.
 
order : str or list of str, optional
When a is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties.
 
Returns: 
index_array : ndarray, int
Array of indices that sort a along the specified axis. If a is one-dimensional, a[index_array] yields a sorted a. More generally, np.take_along_axis(a, index_array, axis=a) always yields the sorted a, irrespective of dimensionality.

示例:

import numpy as np
top_k=3
arr = np.array([1, 3, 2, 4, 5])
top_k_idx=arr.argsort()[::-1][0:top_k]
print(top_k_idx)
#[4 3 1]

补充:python topN / topK 取 最大的N个数 或 最小的N个数

import numpy as np
a = np.array([1,4,3,5,2])
b = np.argsort(a)
print(b)

print结果[0 4 2 1 3]

说明a[0]最小,a[3]最大

a[0]<a[4]<a[2]<a[1]<a[3]

补充:利用Python获取数组或列表中最大的N个数及其索引

看代码吧~

import heapq
 
a=[43,5,65,4,5,8,87]
re1 = heapq.nlargest(3, a) #求最大的三个元素,并排序
re2 = map(a.index, heapq.nlargest(3, a)) #求最大的三个索引    nsmallest与nlargest相反,求最小
print(re1)
print(list(re2)) #因为re2由map()生成的不是list,直接print不出来,添加list()就行了

结果:

re1:[87, 65, 43]

re2:[6, 2, 0]

以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
详解Python爬虫的基本写法
Jan 08 Python
Python实现读写sqlite3数据库并将统计数据写入Excel的方法示例
Aug 07 Python
pandas创建新Dataframe并添加多行的实例
Apr 08 Python
转换科学计数法的数值字符串为decimal类型的方法
Jul 16 Python
浅谈django三种缓存模式的使用及注意点
Sep 30 Python
Python的条件锁与事件共享详解
Sep 12 Python
python中for循环变量作用域及用法详解
Nov 05 Python
python 实现矩阵按对角线打印
Nov 29 Python
Python简单实现区域生长方式
Jan 16 Python
django序列化时使用外键的真实值操作
Jul 15 Python
Python爬虫与反爬虫大战
Jul 30 Python
用Python制作音乐海报
Jan 26 Python
使用pandas模块实现数据的标准化操作
pandas 实现将NaN转换为None
May 14 #Python
Pandas||过滤缺失数据||pd.dropna()函数的用法说明
Python爬虫:从m3u8文件里提取小视频的正确操作
MATLAB 全景图切割及盒图显示的实现步骤
使用pandas或numpy处理数据中的空值(np.isnan()/pd.isnull())
May 14 #Python
PyQt5爬取12306车票信息程序的实现
You might like
8个必备的PHP功能开发
2015/10/02 PHP
smarty高级特性之对象的使用方法
2015/12/25 PHP
YII2.0之Activeform表单组件用法实例
2016/01/09 PHP
PHP使用Mysqli类库实现完美分页效果的方法
2016/04/07 PHP
Laravel 5.4向IoC容器中添加自定义类的方法示例
2017/08/15 PHP
JQUERY操作JSON实例代码
2010/02/09 Javascript
js 中的switch表达式使用示例
2020/06/03 Javascript
jquery.mousewheel实现整屏翻屏效果
2015/08/30 Javascript
JavaScript中Textarea滚动条不能拖动的解决方法
2015/12/15 Javascript
利用JS实现页面删除并重新排序功能
2016/12/09 Javascript
js实现消息滚动效果
2017/01/18 Javascript
Angular使用$http.jsonp发送跨站请求的方法
2017/03/16 Javascript
基于AngularJS实现的工资计算器实例
2017/06/16 Javascript
JavaScript动态绑定详解
2017/09/14 Javascript
vue和react等项目中更简单的实现展开收起更多等效果示例
2018/02/22 Javascript
JS实现获取自定义属性data值的方法示例
2018/12/19 Javascript
图解javascript作用域链
2019/05/27 Javascript
详解Vue中的基本语法和常用指令
2019/07/23 Javascript
vue 中的动态传参和query传参操作
2020/11/09 Javascript
[01:08:10]2014 DOTA2国际邀请赛中国区预选赛 SPD-GAMING VS LGD-CDEC
2014/05/22 DOTA
Python常用知识点汇总
2016/05/08 Python
Python实现自动登录百度空间的方法
2017/06/10 Python
Python计算斗牛游戏概率算法实例分析
2017/09/26 Python
Python随机函数库random的使用方法详解
2019/08/21 Python
基于pandas向csv添加新的行和列
2020/05/25 Python
浅谈Selenium+Webdriver 常用的元素定位方式
2021/01/13 Python
HTML 5.1来了 9月份正式发布 更新内容预览
2016/04/26 HTML / CSS
处理HTML5新标签的浏览器兼容版问题
2017/03/13 HTML / CSS
美国滑板店:Tactics
2020/11/08 全球购物
电子商务专业实习生自我鉴定
2013/09/24 职场文书
求职信写作要突出重点
2014/01/01 职场文书
单位工程竣工验收方案
2014/03/16 职场文书
股东出资证明书(正规版)
2014/09/24 职场文书
营运督导岗位职责
2015/04/10 职场文书
大学生军训感言
2015/08/01 职场文书
使用Golang的channel交叉打印两个数组的操作
2021/04/29 Golang