如何获取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安装Imaging报错:The _imaging C module is not installed问题解决方法
Aug 22 Python
Django URL传递参数的方法总结
Aug 28 Python
Python用list或dict字段模式读取文件的方法
Jan 10 Python
Python3如何解决字符编码问题详解
Apr 23 Python
Python编写一个优美的下载器
Apr 15 Python
PyQt5每天必学之拖放事件
Aug 27 Python
基于Python开发chrome插件的方法分析
Jul 07 Python
python的debug实用工具 pdb详解
Jul 12 Python
Python命名空间namespace及作用域原理解析
Jun 05 Python
Python Serial串口基本操作(收发数据)
Nov 06 Python
利用Pycharm连接服务器的全过程记录
Jul 01 Python
聊聊基于pytorch实现Resnet对本地数据集的训练问题
Mar 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截断标题且兼容utf8和gb2312编码
2013/09/22 PHP
PHP IDE PHPStorm配置支持友好Laravel代码提示方法
2015/05/12 PHP
PHP概率计算函数汇总
2015/09/13 PHP
jQuery 动态酷效果实现总结
2009/12/27 Javascript
图片翻转效果具体实现代码
2014/01/09 Javascript
判断日期是否能跨月查询的js代码
2014/07/25 Javascript
jQueryMobile之Helloworld与页面切换的方法
2015/02/04 Javascript
在React框架中实现一些AngularJS中ng指令的例子
2016/03/06 Javascript
jquery自定义插件——window的实现【示例代码】
2016/05/06 Javascript
javascript对象的相关操作小结
2016/05/16 Javascript
js的函数的按值传递参数(实例讲解)
2017/11/16 Javascript
Vue.js 表单控件操作小结
2018/03/29 Javascript
用vue2.0实现点击选中active其他选项互斥的效果
2018/04/12 Javascript
在vue项目中正确使用iconfont的方法
2018/09/28 Javascript
微信小程序日历插件代码实例
2019/12/04 Javascript
基于jQuery实现可编辑的表格
2019/12/11 jQuery
[52:15]2014 DOTA2国际邀请赛中国区预选赛5.21 HGT VS LGD-GAMING
2014/05/23 DOTA
Python入门篇之对象类型
2014/10/17 Python
Python解决鸡兔同笼问题的方法
2014/12/20 Python
Python实现破解猜数游戏算法示例
2017/09/25 Python
python读取和保存视频文件
2018/04/16 Python
Django处理文件上传File Uploads的实例
2018/05/28 Python
解决pycharm无法识别本地site-packages的问题
2018/10/13 Python
python批量赋值操作实例
2018/10/22 Python
python爬取Ajax动态加载网页过程解析
2019/09/05 Python
wxPython实现整点报时
2019/11/18 Python
pandas实现excel中的数据透视表和Vlookup函数功能代码
2020/02/14 Python
浅谈SciPy中的optimize.minimize实现受限优化问题
2020/02/29 Python
使用phonegap克隆和删除联系人的实现方法
2017/03/31 HTML / CSS
Space NK美国站:英国高端美妆护肤商城
2017/05/22 全球购物
法律专业求职信
2014/05/24 职场文书
学校端午节活动总结
2015/02/11 职场文书
2015年财务部年度工作总结
2015/05/19 职场文书
用React Native制作一个简单的游戏引擎
2021/05/27 Javascript
Golang 链表的学习和使用
2022/04/19 Golang
vue3不同环境下实现配置代理
2022/05/25 Vue.js