如何获取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中的函数用法入门教程
Sep 02 Python
python3+PyQt5实现使用剪贴板做复制与粘帖示例
Jan 24 Python
利用Pandas读取文件路径或文件名称包含中文的csv文件方法
Jul 04 Python
利用Python求阴影部分的面积实例代码
Dec 05 Python
在python里从协程返回一个值的示例
Feb 19 Python
详解Python3 pandas.merge用法
Sep 05 Python
TensorFlow索引与切片的实现方法
Nov 20 Python
tensorflow 实现打印pb模型的所有节点
Jan 23 Python
使用Keras预训练模型ResNet50进行图像分类方式
May 23 Python
Python基于smtplib协议实现发送邮件
Jun 03 Python
Python pandas对excel的操作实现示例
Jul 21 Python
jupyter notebook 写代码自动补全的实现
Nov 02 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实现 上一篇、下一篇的代码
2012/09/29 PHP
浅析PHP程序防止ddos,dns,集群服务器攻击的解决办法
2013/06/18 PHP
简单谈谈php中ob_flush和flush的区别
2014/11/27 PHP
PHP设置进度条的方法
2015/07/08 PHP
PHP开发制作一个简单的活动日程表Calendar
2016/06/20 PHP
mysql查找删除重复数据并只保留一条实例详解
2016/09/24 PHP
Laravel6.18.19如何优雅的切换发件账户
2020/06/14 PHP
jquery下组织javascript代码(js函数化)
2010/08/25 Javascript
Javascript 倒计时源代码.(时.分.秒) 详细注释版
2011/05/09 Javascript
解决JS浮点数运算出现Bug的方法
2013/03/12 Javascript
Node.js模块加载详解
2014/08/16 Javascript
详解基于angular路由的requireJs按需加载js
2017/01/20 Javascript
three.js快速入门【推荐】
2017/01/21 Javascript
input 标签实现输入框带提示文字效果(两种方法)
2017/10/09 Javascript
使用3D引擎threeJS实现星空粒子移动效果
2020/09/13 Javascript
微信小程序倒计时功能实例代码
2018/07/17 Javascript
Angular Material Icon使用详解
2018/11/07 Javascript
JointJS流程图的绘制方法
2018/12/03 Javascript
基于wxpython实现的windows GUI程序实例
2015/05/30 Python
PyQt实现界面翻转切换效果
2018/04/20 Python
利用Python实现在同一网络中的本地文件共享方法
2018/06/04 Python
详解python之协程gevent模块
2018/06/14 Python
python脚本调用iftop 统计业务应用流量的思路详解
2019/10/11 Python
Pytorch中.new()的作用详解
2020/02/18 Python
Python常用库Numpy进行矩阵运算详解
2020/07/21 Python
CSS3中currentColor关键字的妙用
2016/02/27 HTML / CSS
英国助听器购物网站:Hearing Direct
2018/08/21 全球购物
Converse匡威法国官网:美国著名帆布鞋品牌
2018/12/05 全球购物
美国领先的家庭健康检测试剂盒提供商:LetsGetChecked
2019/03/18 全球购物
财务经理岗位职责
2013/11/09 职场文书
学生会副主席竞聘书
2014/03/31 职场文书
个人授权委托书
2014/04/03 职场文书
会计求职自荐信范文
2015/03/04 职场文书
居委会工作总结2015
2015/05/18 职场文书
2016廉洁从业学习心得体会
2016/01/19 职场文书
PYTHON 使用 Pandas 删除某列指定值所在的行
2022/04/28 Python