如何获取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中zip()函数用法实例教程
Jul 31 Python
Python的Flask框架中的Jinja2模板引擎学习教程
Jun 30 Python
代码讲解Python对Windows服务进行监控
Feb 11 Python
Python中函数参数调用方式分析
Aug 09 Python
python pygame实现五子棋小游戏
Oct 26 Python
python防止随意修改类属性的实现方法
Aug 21 Python
pygame实现烟雨蒙蒙下彩虹雨
Nov 11 Python
Python实现新型冠状病毒传播模型及预测代码实例
Feb 05 Python
python str字符串转uuid实例
Mar 03 Python
python删除某个目录文件夹的方法
May 26 Python
关于python中导入文件到list的问题
Oct 31 Python
Python借助with语句实现代码段只执行有限次
Mar 23 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
收听短波不可能有声音清晰的品质吗
2021/03/01 无线电
php数组应用之比较两个时间的相减排序
2008/08/18 PHP
php知道与问问的采集插件代码
2010/10/12 PHP
php中使用preg_match_all匹配文章中的图片
2013/02/06 PHP
浅析十款PHP开发框架的对比
2013/07/05 PHP
php实现递归的三种基本方式
2020/07/04 PHP
PHP编写RESTful接口
2016/02/23 PHP
简介PHP的Yii框架中缓存的一些高级用法
2016/03/29 PHP
详解php中的implements 使用
2017/06/13 PHP
基于swoole实现多人聊天室
2018/06/14 PHP
PHP数据对象映射模式实例分析
2019/03/29 PHP
对象的类型:本地对象(1)
2006/12/29 Javascript
A标签触发onclick事件而不跳转的多种解决方法
2013/06/27 Javascript
Node.js实现批量去除BOM文件头
2014/12/20 Javascript
简述AngularJS的控制器的使用
2015/06/16 Javascript
一篇文章掌握RequireJS常用知识
2016/01/26 Javascript
yarn与npm的命令行小结
2016/10/20 Javascript
jquery dialog获取焦点的方法
2017/02/09 Javascript
vue过渡和animate.css结合使用详解
2017/06/14 Javascript
Vue.js+Layer表格数据绑定与实现更新的实例
2018/03/07 Javascript
详解Vue源码学习之双向绑定
2019/04/10 Javascript
python机器学习之神经网络实现
2018/10/13 Python
Python输出\u编码将其转换成中文的实例
2018/12/15 Python
HTML5单页面手势滑屏切换原理
2016/03/21 HTML / CSS
iphoneX 适配客户端H5页面的方法教程
2017/12/08 HTML / CSS
navabi英国:设计师大码女装
2019/06/25 全球购物
Overload和Override的区别
2012/09/02 面试题
中专毕业个人的自荐信格式
2013/09/21 职场文书
保护地球的标语
2014/06/17 职场文书
做一个有道德的人活动实施方案
2014/08/23 职场文书
公证委托书标准格式
2014/09/11 职场文书
基层党员学习党的群众路线教育实践活动心得体会
2014/11/04 职场文书
2015年财务个人工作总结范文
2015/05/22 职场文书
毕业生政审意见范文
2015/06/04 职场文书
母亲节主题班会
2015/08/14 职场文书
磁贴还没死, 微软Win11可修改注册表找回Win10开始菜单
2021/11/21 数码科技