如何获取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处理文本文件并生成指定格式的文件
Jul 31 Python
Python实现SVN的目录周期性备份实例
Jul 17 Python
python爬虫中get和post方法介绍以及cookie作用
Feb 08 Python
python将文本分每两行一组并保存到文件
Mar 19 Python
python如何爬取个性签名
Jun 19 Python
Python 获取ftp服务器文件时间的方法
Jul 02 Python
python绘制多个子图的实例
Jul 07 Python
Django的性能优化实现解析
Jul 30 Python
解决Pytorch训练过程中loss不下降的问题
Jan 02 Python
python爬虫泛滥的解决方法详解
Nov 25 Python
解决pycharm修改代码后第一次运行不生效的问题
Feb 06 Python
Python图像处理之图像拼接
Apr 28 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符合PSR编程规范的实例分享
2016/12/21 PHP
php实现的redis缓存类定义与使用方法示例
2017/08/09 PHP
选择TreeView控件的树状数据节点的JS方法(jquery)
2010/02/06 Javascript
javascript 事件处理示例分享
2014/12/31 Javascript
JQuery解析XML数据的几个简单实例
2016/05/18 Javascript
基于JS代码实现图片在页面中旋转效果
2016/06/16 Javascript
jQuery EasyUI菜单与按钮详解
2016/07/13 Javascript
微信小程序 密码输入(源码下载)
2017/06/27 Javascript
jQuery中将json数据显示到页面表格的方法
2018/05/27 jQuery
vue.js 中使用(...)运算符报错的解决方法
2018/08/09 Javascript
three.js实现圆柱体
2018/12/30 Javascript
VUE UPLOAD 通过ACTION返回上传结果操作
2020/09/07 Javascript
python fabric使用笔记
2015/05/09 Python
python中管道用法入门实例
2015/06/04 Python
Python 使用with上下文实现计时功能
2018/03/09 Python
python2.7无法使用pip的解决方法(安装easy_install)
2018/04/03 Python
在Python中如何传递任意数量的实参的示例代码
2019/03/21 Python
打包python 加icon 去掉cmd黑窗口方法
2019/06/24 Python
使用python matplotlib 画图导入到word中如何保证分辨率
2020/04/16 Python
Python使用configparser读取ini配置文件
2020/05/25 Python
使用darknet框架的imagenet数据分类预训练操作
2020/07/07 Python
Python requests及aiohttp速度对比代码实例
2020/07/16 Python
css3中的calc函数浅析
2018/07/10 HTML / CSS
css3+jq创作含苞待放的荷花
2014/02/20 HTML / CSS
英国最大的女士服装零售商:Bonmarché
2017/08/17 全球购物
网络体系结构及协议的定义
2014/03/13 面试题
网络工程系信息安全技术专业大学生求职信
2013/10/22 职场文书
单位单身证明范本
2014/01/11 职场文书
奶茶店创业计划书范文
2014/01/17 职场文书
高中政治教学反思
2014/01/18 职场文书
触摸春天教学反思
2014/02/03 职场文书
关于读书的演讲稿400字
2014/08/27 职场文书
党员干部批评与自我批评反四风思想汇报
2014/09/21 职场文书
学校禁毒宣传活动总结
2015/05/08 职场文书
《分数的意义》教学反思
2016/02/20 职场文书
Spring Cache和EhCache实现缓存管理方式
2021/06/15 Java/Android