如何获取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中的urllib2模块
Nov 13 Python
python各种语言间时间的转化实现代码
Mar 23 Python
解决Scrapy安装错误:Microsoft Visual C++ 14.0 is required...
Oct 01 Python
python Web开发你要理解的WSGI &amp; uwsgi详解
Aug 01 Python
对pandas的行列名更改与数据选择详解
Nov 12 Python
python存储16bit和32bit图像的实例
Dec 05 Python
python paramiko利用sftp上传目录到远程的实例
Jan 03 Python
详解python数据结构和算法
Apr 18 Python
pyqt5 删除layout中的所有widget方法
Jun 25 Python
在python中利用dict转json按输入顺序输出内容方式
Feb 27 Python
通过代码实例了解Python异常本质
Sep 16 Python
Python抓包并解析json爬虫的完整实例代码
Nov 03 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获取类中常量,属性,及方法列表的方法
2009/04/09 PHP
php设计模式 Delegation(委托模式)
2011/06/26 PHP
php中创建和调用webservice接口示例
2014/07/25 PHP
JQuery Study Notes 学习笔记(一)
2010/08/04 Javascript
Jquery实现的tab效果可以指定默认显示第几页
2013/10/16 Javascript
浅谈Jquery为元素绑定事件
2015/04/27 Javascript
JavaScript入门基础
2015/08/12 Javascript
基于bootstrap插件实现autocomplete自动完成表单
2016/05/07 Javascript
微信小程序实现拖拽 image 触摸事件监听的实例
2017/08/17 Javascript
vue对storejs获取的数据进行处理时遇到的几种问题小结
2018/03/20 Javascript
Vue实现跑马灯效果
2020/05/25 Javascript
前端vue+elementUI如何实现记住密码功能
2020/09/20 Javascript
解决vue2中使用elementUi打包报错的问题
2020/09/22 Javascript
JS hasOwnProperty()方法检测一个属性是否是对象的自有属性的方法
2021/01/29 Javascript
[00:34]拔城逐梦,热血永恒!2020(秋)完美世界城市挑战赛报名开启
2020/10/09 DOTA
[43:26]完美世界DOTA2联赛PWL S2 Forest vs Rebirth 第二场 11.20
2020/11/23 DOTA
linux系统使用python监测网络接口获取网络的输入输出
2014/01/15 Python
python中的yield使用方法
2014/02/11 Python
python获取当前计算机cpu数量的方法
2015/04/18 Python
python dataframe常见操作方法:实现取行、列、切片、统计特征值
2018/06/09 Python
Python3实现的判断回文链表算法示例
2019/03/08 Python
python实现PCA降维的示例详解
2020/02/24 Python
基于python实现可视化生成二维码工具
2020/07/08 Python
Python命名空间及作用域原理实例解析
2020/08/12 Python
python七种方法判断字符串是否包含子串
2020/08/18 Python
python爬取音频下载的示例代码
2020/10/19 Python
python中的插入排序的简单用法
2021/01/19 Python
Jacadi Paris美国官方网站:法国童装品牌
2017/10/15 全球购物
全球性的在线商店:Vogca
2019/05/10 全球购物
Lungolivigno Fashion官网:高级时装在线购物
2020/10/17 全球购物
什么时候用assert
2015/05/08 面试题
浙江文明网签名寄语
2014/01/18 职场文书
五年级数学教学反思
2014/02/11 职场文书
党员学习中共十八大思想报告
2014/09/12 职场文书
店铺转让协议书
2015/01/29 职场文书
一篇合格的广告文案,其主要目的是什么?
2019/07/12 职场文书