如何获取numpy array前N个最大值
作者:upDiff 发布时间:2022-02-08 18:48:28
标签:numpy,array,最大值
主要应用了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]
来源:https://blog.csdn.net/dlhlSC/article/details/88072268


猜你喜欢
- 目录1.技术背景2.问题复现3.解决思路4.总结概要1.技术背景笔者在执行一个Jax的任务中,又发现了一个奇怪的问题,就是明明只分配了很小的
- 目录概述索引数据结构二叉树红黑树B-TreeB+TreeHash索引InnoDB 索引实现(聚集)索引文件和数据文件是分离的(非聚集)聚集索
- 前言搞了一台云服务器,首先要干的活就是得安装数据库,在Windows下安装不用说,傻瓜式操作,在Linux上安装少说要记录一下。我使用的是X
- 1:除非你现在已经过了不惑之年了,否则你就一定要保持年轻人特有的激情!这里的激情,包含了那种说不明白的近似于冲动的东西,或者idea。也包含
- 需求说明:通过在界面上输入春联的上、下批和横批汉字从而生成春联图像,最后将春联图片保存。有实际需要的还可以将春联打印。实现过程:实现思路是先
- 1、概述Golang是一种强类型语言,虽然在代码中经常看到i:=12这种写法,这其实是编译器在编译期间自动做了类型推断。编译器会对数据进行类
- 01、文件操作文件是操作系统提供给用户/应用程序操作硬盘的一个虚拟的概念/接口用户/应用程序可以通过文件将数据永久保存在硬盘中用户/应用程序
- 经常使用python检测服务器是否能ping通, 程序是否正常运行(检测对应的端口是否正常)以前使用shell脚本的写法如下:PINGRET
- 概述不知从何时起,Python和爬虫就如初恋一般,情不知所起,一往而深,相信很多朋友学习Python,都是从爬虫开始,其实究其原因,不外两方
- Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。在系统
- 有一张错误上报表,下面只将与本文相关的字段罗列如下:上报人(ReportPerson)、上报错误ID(ErrorID)、上报时间(Repor
- 先让我们看一个例子,了解什么是模式化窗口。以下是QQ秀商城在非登录时提示登录的一种状态。当我在非登录状态,通过保存形象的方式买一件衣服时,弹
- 我们很少会一次性从数据库中取出所有的数据;通常都只针对一部分数据进行操作。 在Django API中,我们可以使用`` filter()``
- 引言我们日常开发中,如何保证接口数据的安全性呢?个人觉得,接口数据安全的保证过程,主要体现在这几个方面:一个就是数据传输过程中的安全,还有就
- 首先编写py程序:printtest.pydef test(): print('print test')将以上.
- arguments定义所有的函数都有一个自己的arguments对象,用来储存它实际接受到的参数,而不局限于函数声明时所定义的参数列表。它不
- mysql之alter表的SQL语句集合,包括增加、修改、删除字段,重命名表,添加、删除主键等。1:删除列ALTER TABLE 【表名字】
- 前言在Django使用Celery异步发送邮件的过程中,遇到Celery日志提示任务已接收,但实际上任务并没有执行,解决后特此记录。使用版本
- 实例如下所示:>>> import pandas as pd>>> df = pd.DataFrame(
- python 中sorted与sort有什么区别sort(cmp=None, key=None, reverse=False)sorted(