如何获取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笔记(叁)继续学习
Oct 24 Python
Python中用Spark模块的使用教程
Apr 13 Python
Python通过poll实现异步IO的方法
Jun 04 Python
python友情链接检查方法
Jul 08 Python
浅析AST抽象语法树及Python代码实现
Jun 06 Python
python 不以科学计数法输出的方法
Jul 16 Python
Python3 实现串口两进程同时读写
Jun 12 Python
Python for循环搭配else常见问题解决
Feb 11 Python
浅析python表达式4+0.5值的数据类型
Feb 26 Python
python3:excel操作之读取数据并返回字典 + 写入的案例
Sep 01 Python
Django如何创作一个简单的最小程序
May 12 Python
python用tkinter开发的扫雷游戏
Jun 01 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获取网页内容方法总结
2008/12/04 PHP
PHP获取Exif缩略图的方法
2015/07/13 PHP
PHPStrom中实用的功能和快捷键大全
2015/09/23 PHP
PHP使用redis实现统计缓存mysql压力的方法
2015/11/14 PHP
JS实现div内部的文字或图片自动循环滚动代码
2013/04/19 Javascript
js+css实现的简单易用兼容好的分页
2013/12/30 Javascript
JS实现文字链接感应鼠标淡入淡出改变颜色的方法
2015/02/26 Javascript
javascript处理a标签超链接默认事件的方法
2015/06/29 Javascript
js 判断所选时间(或者当前时间)是否在某一时间段的实现代码
2015/09/05 Javascript
JavaScript获取浏览器信息的方法
2015/11/20 Javascript
jQuery 操作input中radio的技巧
2016/07/18 Javascript
基于jPlayer三分屏的制作方法
2016/12/21 Javascript
基于JS实现移动端左滑删除功能
2017/07/28 Javascript
你可能不知道的JSON.stringify()详解
2017/08/17 Javascript
JavaScript动态绑定详解
2017/09/14 Javascript
在React 组件中使用Echarts的示例代码
2017/11/08 Javascript
Vue实现计算器计算效果
2020/08/17 Javascript
让Vue响应Map或Set的变化操作
2020/11/11 Javascript
javascript实现搜索筛选功能实例代码
2020/11/12 Javascript
使用原生javascript开发计算器实例代码
2021/02/21 Javascript
[01:02:20]Mineski vs TNC 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
Python threading多线程编程实例
2014/09/18 Python
利用python实现命令行有道词典的方法示例
2017/01/31 Python
python配置grpc环境
2019/01/01 Python
莫斯科的韩国化妆品店:Sifo
2019/12/04 全球购物
师范生实习的个人自我鉴定
2013/10/20 职场文书
生物专业个人自荐信范文
2013/11/29 职场文书
初婚未育证明
2014/01/15 职场文书
实习生求职自荐信
2014/02/07 职场文书
电子信息工程自荐信
2014/05/26 职场文书
法定代表人授权委托书范文
2014/09/22 职场文书
现役军人家属慰问信
2015/03/24 职场文书
离职证明格式样本
2015/06/12 职场文书
2016银行招聘自荐信
2016/01/28 职场文书
react中的DOM操作实现
2021/06/30 Javascript
SQL Server中的逻辑函数介绍
2022/05/25 SQL Server