如何获取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实现逆波兰计算表达式实例详解
May 06 Python
Python线程指南详细介绍
Jan 05 Python
python 3利用Dlib 19.7实现摄像头人脸检测特征点标定
Feb 26 Python
Python实现线程状态监测简单示例
Mar 28 Python
python smtplib模块自动收发邮件功能(二)
May 22 Python
Python实现爬虫抓取与读写、追加到excel文件操作示例
Jun 27 Python
给 TensorFlow 变量进行赋值的方式
Feb 10 Python
python thrift 实现 单端口多服务的过程
Jun 08 Python
使用python爬取抖音app视频的实例代码
Dec 01 Python
浅析Python模块之间的相互引用问题
Feb 26 Python
python中使用asyncio实现异步IO实例分析
Feb 26 Python
使用pipenv管理python虚拟环境的全过程
Sep 25 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
php+mysql 实现身份验证代码
2010/03/24 PHP
浅析PHP的静态成员函数效率更高的原因
2014/06/13 PHP
PHP时间和日期函数详解
2015/05/08 PHP
php获取json数据所有的节点路径
2015/05/17 PHP
通过js获取div的background-image属性
2013/10/15 Javascript
jquery实现可拖动DIV自定义保存到数据的实例
2013/11/20 Javascript
jquery中JSON的解析方式
2015/03/16 Javascript
javascript用函数实现对象的方法
2015/05/14 Javascript
jquery实现图片放大镜功能
2015/11/23 Javascript
JS 动态判断PC和手机浏览器实现代码
2016/09/21 Javascript
RGB和YUV 多媒体编程基础详细介绍
2016/11/04 Javascript
Java与JavaScript中判断两字符串是否相等的区别
2017/03/13 Javascript
Bootstrap下拉菜单Dropdowns的实现代码
2017/03/17 Javascript
如何从零开始利用js手写一个Promise库详解
2018/04/19 Javascript
Vue监听事件实现计数点击依次增加的方法
2018/09/26 Javascript
JavaScript实现烟花绽放动画效果
2020/08/04 Javascript
Python os模块介绍
2014/11/30 Python
简单了解Python下用于监视文件系统的pyinotify包
2015/11/13 Python
python切片及sys.argv[]用法详解
2018/05/25 Python
Python中fnmatch模块的使用详情
2018/11/30 Python
Django 创建新App及其常用命令的实现方法
2019/08/04 Python
Python字典生成式、集合生成式、生成器用法实例分析
2020/01/07 Python
tensorflow 保存模型和取出中间权重例子
2020/01/24 Python
浅谈tensorflow模型保存为pb的各种姿势
2020/05/25 Python
python使用nibabel和sitk读取保存nii.gz文件实例
2020/07/01 Python
Scrapy模拟登录赶集网的实现代码
2020/07/07 Python
python如何快速拼接字符串
2020/10/28 Python
HTML5之tabindex属性全面解析
2016/07/07 HTML / CSS
什么是继承
2013/12/07 面试题
办公室副主任岗位职责
2013/11/25 职场文书
企业后勤岗位职责
2014/02/28 职场文书
公司自我介绍演讲稿
2014/08/21 职场文书
优秀教师申报材料
2014/12/16 职场文书
贷款收入证明范本
2015/06/12 职场文书
趣味运动会赞词
2015/07/22 职场文书
读《庄子》有感:美而不自知
2019/11/06 职场文书