如何获取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 相关文章推荐
gearman的安装启动及python API使用实例
Jul 08 Python
跟老齐学Python之从格式化表达式到方法
Sep 28 Python
详解Python中用于计算指数的exp()方法
May 14 Python
Python字符串格式化
Jun 15 Python
Python中type的构造函数参数含义说明
Jun 21 Python
Python实现的HMacMD5加密算法示例
Apr 03 Python
python 标准差计算的实现(std)
Jul 29 Python
Python csv模块使用方法代码实例
Aug 29 Python
python logging日志模块原理及操作解析
Oct 12 Python
wxpython+pymysql实现用户登陆功能
Nov 19 Python
python Scrapy框架原理解析
Jan 04 Python
python实现录制全屏和选择区域录屏功能
Feb 05 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
PhpMyAdmin中无法导入sql文件的解决办法
2010/01/08 PHP
10个实用的PHP正则表达式汇总
2014/10/23 PHP
PHP的Yii框架中YiiBase入口类的扩展写法示例
2016/03/17 PHP
js以对象为索引的关联数组
2010/07/04 Javascript
JS实现文字链接感应鼠标淡入淡出改变颜色的方法
2015/02/26 Javascript
手机图片预览插件photoswipe.js使用总结
2016/08/25 Javascript
jQuery实现的图片轮播效果完整示例
2016/09/12 Javascript
用瀑布流的方式在网页上插入图片的简单实现方法
2016/09/23 Javascript
微信小程序 获取设备信息 API实例详解
2016/10/02 Javascript
Vue中的混入的使用(vue mixins)
2018/06/01 Javascript
详解vue axios用post提交的数据格式
2018/08/07 Javascript
express 项目分层实践详解
2018/12/10 Javascript
Vue项目打包部署到iis服务器的配置方法
2019/10/14 Javascript
微信小程序跨页面传递data数据方法解析
2019/12/13 Javascript
解决qrcode.js生成二维码时必须定义一个空div的问题
2020/07/09 Javascript
Vue清除定时器setInterval优化方案分享
2020/07/21 Javascript
[00:17]DOTA2荣耀之路5:It’s a disastah!
2018/05/28 DOTA
深入解读Python解析XML的几种方式
2016/02/16 Python
Python编程实现的图片识别功能示例
2017/08/03 Python
PyQt5每天必学之拖放事件
2020/08/27 Python
python 实现将小图片放到另一个较大的白色或黑色背景图片中
2019/12/12 Python
编写python代码实现简单抽奖器
2020/10/20 Python
两种CSS3伪类选择器详细介绍
2013/12/24 HTML / CSS
当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
2014/09/09 面试题
幼师专业毕业生自荐信
2013/09/29 职场文书
学生会干部自荐信
2014/02/04 职场文书
房地产广告策划方案
2014/05/15 职场文书
大学生第一学年自我鉴定2015
2014/09/28 职场文书
高二化学教学反思
2016/02/22 职场文书
人身损害赔偿协议书
2016/03/22 职场文书
2019自荐信该如何写呢?
2019/07/05 职场文书
2019财务管理制度最新范本!
2019/07/09 职场文书
三年级作文之趣事作文
2019/11/04 职场文书
企业内部管理控制:采购授权审批制度范本
2020/01/19 职场文书
Nginx 负载均衡是什么以及该如何配置
2021/03/31 Servers
python实现进度条的多种实现
2021/04/29 Python