numpy.argsort()函数

news/2024/10/19 21:37:26/

numpy.argsort()函数

  • numpy.argsort() 函数用于使用关键字kind指定的算法沿给定轴执行间接排序。 它返回一个与 arr 形状相同的索引数组,用于对数组进行排序,按升序排列

1.函数原型:

numpy.argsort(arr, axis=-1, kind=’quicksort’, order=None) 

2.参数

  • arr:[array_like],输入数组
  • axis:[int or None],排序的轴, 如果没有,数组在排序前被展平。 默认值为 -1,即沿最后一个轴排序。
  • kind:[‘quicksort’, ‘mergesort’, ‘heapsort’],选择算法, 默认为“快速排序”。
  • order : [str or list of str] ,当 arr 是一个定义了字段的数组时,这个参数指定首先比较哪些字段,第二个等等。
  • return: [index_array, ndarray] ,沿指定轴对 arr 排序的索引数组。如果 arr 是一维的,则 arr[index_array] 返回排序后的 arr。

示例1

import numpy as np
# input array
in_arr = np.array([2, -1, 9, 0, 6, 40])
print("Input unsorted array : ", in_arr)out_arr = np.argsort(in_arr)
print("Output sorted array indices : ", out_arr)
print("Output sorted array : ", in_arr[out_arr])
  • 输出
Input unsorted array :  [ 2 -1  9  0  6 40]
Output sorted array indices :  [1 3 0 4 2 5]
Output sorted array :  [-1  0  2  6  9 40]

示例2

import numpy as np# input 2d arrayin_arr = np.array([[2, 0, 10],[5, 4, 3]])
print("input array : ", in_arr)# output sorted array indices
out_arr1 = np.argsort(in_arr, kind='mergesort', axis=0) # 列
print("Output sorted array indices along axis 0: ", out_arr1)
out_arr2 = np.argsort(in_arr, kind='heapsort', axis=1) # 行
print("Output sorteded array indices along axis 1: ", out_arr2)
  • 输出
input array :  [[ 2  0 10][ 5  4  3]]
Output sorted array indices along axis 0:  [[0 0 1][1 1 0]]
Output sorteded array indices along axis 1:  [[1 0 2][2 1 0]]

示例3

# get two largest value from numpy array
x=np.array([12,43,2,100,54,5,68])
print(x)
# using argsort get indices of value of arranged in ascending order
print(np.argsort(x))
#get two highest value index of array
print(np.argsort(x)[-2:])
# to arrange in ascending order of index
print(np.argsort(x)[-2:][::-1])
# to get highest 2 values from array
x[np.argsort(x)[-2:][::-1]]
  • 输出
[ 12  43   2 100  54   5  68]
[2 5 0 1 4 6 3]
[6 3]
[3 6]
[100  68]

http://www.ppmy.cn/news/317258.html

相关文章

【03 yy and shop】

03 yy and shop 题目解法C 题目 解法 C #include<bits/stdc.h> using namespace std; const int M 1000; int C[800],V[800],K[800]; int F[801][3001], G[801][3001];int main(){int N;scanf("%d",&N);for(int i1; i<N; i) scanf("%d%d%d&quo…

np.ascontiguousarray(array)

从Numpy中的ascontiguousarray说起 Numpy中&#xff0c;随机初始化的数组默认都是C连续的&#xff0c;经过不规则的slice操作&#xff0c;则会改变连续性&#xff0c;可能会变成既不是C连续&#xff0c;也不是Fortran连续的。 Numpy可以通过.flags熟悉查看一个数组是C连续还是…

numpy.argsort()函数详解

官方文档1 numpy.argsort(a, axis-1, kindquicksort, orderNone) 返回一个排序后的数组的索引。 执行一个由kind参数指定的方式排列。 Parameters a : array_like 需要被处理的数组 axis : int or None, optional 排序的轴向&#xff0c;默认-1&#xff0c;the last axis ki…

python中:TypeError: only size-1 arrays can be converted to Python scalars

这里写自定义目录标题 Python中的math和numpy源程序修改后错误原因 Python中的math和numpy 错误描述&#xff1a;TypeError: only size-1 arrays can be converted to Python scalars 源程序 修改后 错误原因 numpy包中也含有sin(),pi需要使用numpy包中函数&#xff1b; 这个…

Python3 TypeError: only size-1 arrays can be converted to Python scalars

问题&#xff1a;在用python3使用knn.train(trainData, responses)的时候&#xff0c;可能会产生错误&#xff1a;TypeError: only size-1 arrays can be converted to Python scalars newcomer np.random.randint(0, 100, (1, 2)).astype(np.float32) plt.scatter(newcomer[…

python np.argsort()(::-1)

python np.argsort()(::-1) 定义一个array数据。 import numpy as np xnp.array([1,4,3,-1,6,9])现在我们可以看看argsort()函数的具体功能是什么&#xff1a; x.argsort() #输出x中元素从小到大排列的对应的index(索引) array([3, 0, 2, 1, 4, 5], dtypeint64)输出定义为ya…

np.argsort 用法总结

np.argsort 本文是根据 python 自带的 np.argsort 代码示例整理总结&#xff0c;博主自认为目前应该是对该函数比较全面的一个解释介绍。若有不当之处请留言。 def argsort(a, axis-1, kindquicksort, orderNone):"""返回对数组排序的索引。使用 “kind” 关键…

sizeof 数组与指针

sizeof的定义&#xff1a; sizeof是C/C中的一个操作符&#xff08;operator&#xff09;&#xff0c;简单的说其作用就是返回一个对象或者类型所占的内存字节数。 MSDN上的解释为&#xff1a; The sizeof keyword gives the amount of storage, in bytes, associated with a…