如何获取numpy array前N个最大值

主要应用了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]

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Python 实现Numpy中找出array中最大值所对应的行和列

    Python特别灵活,肯定方法不止一种,这里介绍一种我觉得比较简单的方法. 如下图,使用x == np.max(x) 获得一个掩模矩阵,然后使用where方法即可返回最大值对应的行和列. where返回一个长度为2的元组,第一个元素保存的是行号,第二个元素保存的是列号. 以上这篇Python 实现Numpy中找出array中最大值所对应的行和列就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

  • numpy找出array中的最大值,最小值实例

    在python中利用numpy创建一个array, 然后我们想获取array的最大值,最小值.可以使用一下方法: 一.创建数组 这样就可以获得一个array的最大值和最小值了. 并且可以利用np.where(np.max(a))来获得最大值,最小值的行和列数. 二.python下对文件的操作 1.移动一个文件夹到另一个文件夹下 首先 import os, skutil 复制文件: shutil.copyfile('oldfile', 'newfile') oldfile和newfile都只能是文

  • python如何获得list或numpy数组中最大元素对应的索引

    获得list中最大元素的索引 aa = [1,2,3,4,5] aa.index(max(aa)) 相应的最小值使用 aa = [1,2,3,4,5] aa.index(min(aa)) 获得numpy数组中最大元素的索引 1.可以使用numpy的函数,argmax获得最大元素的索引,相应的获得最小值的话需要使用argmin. aa = [1,2,3,4,5] arr_aa = np.array(aa) maxindex = np.argmax(arr_aa ) 1.也可以将numpy转为lis

  • python中找出numpy array数组的最值及其索引方法

    在list列表中,max(list)可以得到list的最大值,list.index(max(list))可以得到最大值对应的索引 但在numpy中的array没有index方法,取而代之的是where,其又是list没有的 首先我们可以得到array在全局和每行每列的最大值(最小值同理) >>> a = np.arange(9).reshape((3,3)) >>> a array([[0, 1, 2], [9, 4, 5], [6, 7, 8]]) >>&

  • 如何获取numpy array前N个最大值

    主要应用了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 i

  • Python 获取numpy.array索引值的实例

    举个例子: q=[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] 我想获取其中值等于7的那个值的下标,以便于用于其他计算. 如果使用np.where,如: q=np.arange(0,16,1) g=np.where(q==7) print q print g 运行结果是: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] (array([7]),) 显然(array([7]),)中的数字7我是没法提取出来做运算的,这是一个tuple

  • Python获取二维矩阵每列最大值的方法

    因为做项目中间有一个很小的环节需要这个功能,所以就写了一个简单的小函数,下面是具体实现: #!usr/bin/env python #encoding:utf-8 ''' __Author__:沂水寒城 ''' def get_max_value(martix): ''' 得到矩阵中每一列最大的值 ''' res_list=[] for j in range(len(martix[0])): one_list=[] for i in range(len(martix)): one_list.ap

  • 对numpy Array [: ,] 的取值方法详解

    NumPy数组是一个多维数组对象,称为ndarray 创建一个numpy数组,如下所示 import numpy as np x=np.array([[1,2,3],[4,5,6],[7,8,9]]) 使用的方法和python中的元表差不多 print(x[0:2]) >>>[[1 2 3] [4 5 6]] print(x[:]) >>>[[1 2 3] [4 5 6] [7 8 9]] #有些比较复杂逗号的用法,不一定需要掌握,知道是什么意思即可 #有时候希望只取某

  • 浅谈Pandas Series 和 Numpy array中的相同点

    相同点: 可以利用中括号获取元素 s[0] 可以的得到单个元素 或 一个元素切片 s[3,7] 可以遍历 for x in s 可以调用同样的函数获取最大最小值 s.mean()  s.max() 可以用向量运算 <1 + s> 和Numpy一样, Pandas Series 也是用C语言, 因此它比Python列表的运算更快 以上这篇浅谈Pandas Series 和 Numpy array中的相同点就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

  • numpy.array 操作使用简单总结

    import numpy as np numpy.array 常用变量及参数 dtype变量,用来存放数据类型, 创建数组时可以同时指定. shape变量, 存放数组的大小, 这人值是可变的, 只要确保无素个数不变的情况下可以任意修改.(-1为自动适配, 保证个数不变) reshape方法,创建一个改变了形状的数组,与原数组是内存共享的,即都指向同一块内存. 创建数组的方法 np.array([[1, 2, 3, 4],[4, 5, 6, 7], [7, 8, 9, 10]], dtype=np

  • js获取指定字符前/后的字符串简单实例

    如下所示: <!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <script type="text/javascript"> /* string 字符串; str 指定字符; split(),用于把一个字符串分割成字符串数组; split(str)[0],读取

  • mysql获取分组后每组的最大值实例详解

     mysql获取分组后每组的最大值实例详解 1. 测试数据库表如下: create table test ( `id` int not null auto_increment, `name` varchar(20) not null default '', `score` int not null default 0, primary key(`id`) )engine=InnoDB CHARSET=UTF8; 2. 插入如下数据: mysql> select * from test; +---

  • 利用shell获取指定日期前N天的日期

    一.创建测试文件test.sh #!/bin/sh . /etc/profile # 参数: # args[0] ,数据日期,日期格式yyyy-MM-dd # 取30天以前的日期 function get_date_30daysbefore() { sec=`date -d $1 +%s` sec_30daysbefore=$((sec - 86400*30)) days_before=`date -d @$sec_30daysbefore +%F` echo $days_before } if

随机推荐