如何获取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通过colorama模块在控制台输出彩色文字的方法
Mar 19 Python
用Python展示动态规则法用以解决重叠子问题的示例
Apr 02 Python
python安装与使用redis的方法
Apr 19 Python
深入理解Python中的super()方法
Nov 20 Python
python实现数据预处理之填充缺失值的示例
Dec 22 Python
Anaconda2下实现Python2.7和Python3.5的共存方法
Jun 11 Python
python 字符串只保留汉字的方法
Nov 16 Python
利用Python实现微信找房机器人实例教程
Mar 10 Python
用python给自己做一款小说阅读器过程详解
Jul 11 Python
python3实现斐波那契数列(4种方法)
Jul 15 Python
wxPython实现列表增删改查功能
Nov 19 Python
pycharm中选中一个单词替换所有重复单词的实现方法
Nov 17 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 引用(&amp;)详解
2009/11/20 PHP
php根据某字段对多维数组进行排序的方法
2015/03/07 PHP
Laravel配置全局公共函数的方法步骤
2019/05/09 PHP
jQuery中使用each处理json数据
2015/04/23 Javascript
javascript中this的四种用法
2015/05/11 Javascript
javascript单页面手势滑屏切换原理详解
2016/03/21 Javascript
Immutable 在 JavaScript 中的应用
2016/05/02 Javascript
需要牢记的JavaScript基础知识
2016/09/25 Javascript
JavaScript中最常见的三个面试题解析
2017/03/04 Javascript
jQuery简介_动力节点Java学院整理
2017/07/04 jQuery
基于Vue CSR的微前端实现方案实践
2020/05/27 Javascript
[52:20]VP vs VG Supermajor小组赛 B组胜者组决赛 BO3 第一场 6.2
2018/06/03 DOTA
python str与repr的区别
2013/03/23 Python
Python进程通信之匿名管道实例讲解
2015/04/11 Python
python取代netcat过程分析
2018/02/10 Python
pandas 取出表中一列数据所有的值并转换为array类型的方法
2018/04/11 Python
Python高级用法总结
2018/05/26 Python
删除python pandas.DataFrame 的多重index实例
2018/06/08 Python
浅谈CSS3 box-sizing 属性 有趣的盒模型
2019/04/02 HTML / CSS
Html5获取高德地图定位天气的方法
2019/12/26 HTML / CSS
Mamas & Papas沙特阿拉伯:英国最受欢迎的婴儿品牌
2017/11/20 全球购物
英国最大的纸工艺品商店:CraftStash
2018/12/01 全球购物
英国领先的高级美容和在线皮肤诊所:Face the Future
2020/06/17 全球购物
思想专业自荐信范文
2013/12/25 职场文书
一体化教学实施方案
2014/05/10 职场文书
云冈石窟导游词
2015/02/04 职场文书
史上最牛的辞职信
2015/02/28 职场文书
人口与计划生育责任书
2015/05/09 职场文书
2015年教师国培感言
2015/08/01 职场文书
自荐信范文
2019/05/20 职场文书
如何利用Matlab制作一款真正的拼图小游戏
2021/05/11 Python
使用python向MongoDB插入时间字段的操作
2021/05/18 Python
react中的DOM操作实现
2021/06/30 Javascript
「月刊Comic Alive」2022年5月号封面公开
2022/03/21 日漫
Django框架之路由用法
2022/06/10 Python
阿里云服务器(windows)手动部署FTP站点详细教程
2022/08/05 Servers