如何获取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之有容乃大的list(1)
Sep 14 Python
Python实现简单的可逆加密程序实例
Mar 05 Python
Python解析xml中dom元素的方法
Mar 12 Python
Python 解决中文写入Excel时抛异常的问题
May 03 Python
Django管理员账号和密码忘记的完美解决方法
Dec 06 Python
对pytorch网络层结构的数组化详解
Dec 08 Python
Python3实现对列表按元组指定列进行排序的方法分析
Dec 22 Python
对numpy下的轴交换transpose和swapaxes的示例解读
Jun 26 Python
python二元表达式用法
Dec 04 Python
tensorflow2.0与tensorflow1.0的性能区别介绍
Feb 07 Python
Python的控制结构之For、While、If循环问题
Jun 30 Python
分享python函数常见关键字
Apr 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
如何使用PHP获取网络上文件
2006/10/09 PHP
thinkphp模板用法和内容输出实例
2014/11/28 PHP
PHP获取数组最大值下标的方法
2015/05/12 PHP
php代码架构的八点注意事项
2016/01/25 PHP
PHP7创建COOKIE和销毁COOKIE的实例方法
2020/02/03 PHP
jquery.cvtooltip.js 基于jquery的气泡提示插件
2010/11/19 Javascript
jquery的ajax请求全面了解
2013/03/20 Javascript
jQuery之ajax删除详解
2014/02/27 Javascript
jquery禁止输入数字以外的字符的示例(纯数字验证码)
2014/04/10 Javascript
js实现同一页面可多次调用的图片幻灯切换效果
2015/02/28 Javascript
手机图片预览插件photoswipe.js使用总结
2016/08/25 Javascript
js点击按钮实现水波纹效果代码(CSS3和Canves)
2016/09/15 Javascript
javascript实现二叉树的代码
2017/06/08 Javascript
浅谈在react中如何实现扫码枪输入
2018/07/04 Javascript
详解如何在webpack中做预渲染降低首屏空白时间
2018/08/22 Javascript
Vue项目配置跨域访问和代理proxy设置方式
2020/09/08 Javascript
python读取word文档的方法
2015/05/09 Python
python中实现精确的浮点数运算详解
2017/11/02 Python
pycharm+django创建一个搜索网页实例代码
2018/01/24 Python
django输出html内容的实例
2018/05/27 Python
Python控制Firefox方法总结
2019/06/03 Python
pyqt5实现按钮添加背景图片以及背景图片的切换方法
2019/06/13 Python
python使用 zip 同时迭代多个序列示例
2019/07/06 Python
python读取配置文件方式(ini、yaml、xml)
2020/04/09 Python
python中的错误如何查看
2020/07/08 Python
Python 中如何写注释
2020/08/28 Python
HTML5 贪吃蛇游戏实现思路及源代码
2013/09/03 HTML / CSS
利用纯html5绘制出来的一款非常漂亮的时钟
2015/01/04 HTML / CSS
HTML5+WebSocket实现多文件同时上传的实例
2016/12/29 HTML / CSS
BIBLOO波兰:捷克的一家在线服装店
2018/03/09 全球购物
Genny意大利官网:意大利高级时装品牌
2020/04/15 全球购物
追悼会主持词
2014/03/20 职场文书
2014年国庆标语
2014/06/30 职场文书
2015年收银工作总结范文
2015/04/01 职场文书
投标单位介绍信
2015/05/05 职场文书
python实现双向链表原理
2022/05/25 Python