当前位置:网站首页>np.argsort()函数详细解析
np.argsort()函数详细解析
2022-07-30 05:38:00 【向大厂出发】
一、np.argsort()用法:
np.argsort(a, axis=-1, kind='quicksort', order=None)
函数功能:将a中的元素从小到大排列,提取其在排列前对应的index(索引)输出。
二、举例:
1、一维数组
先定义一个array数组x,然后使用函数
import numpy as np
x=np.array([1,4,3,-1,6,9])
y=np.argsort(x)
print('一维数组的排序结果:{}'.format(y))输出结果如下:
一维数组的排序结果:[3 0 2 1 4 5]中间过程详细解释:
①输入为: [1,4,3,-1,6,9],对应索引为:[0,1,2,3,4,5],即
x[0]=1,x[1]=4,x[2]=3,x[3]=-1,x[4]=6,x[5]=9②排序结果为: [-1,1,3,4,6,9]
argsort()是提取排序结果元素在排列前对应的index(索引)输出。
根据排序前x[3]=-1可知,排序结果中,元素-1对应排序前的索引3;
根据排序前x[5]=9可知,排序结果中,元素9对应排序前的索引5;
以此类推,输出 排序后元素在排列前对应的index(索引):
即:[3,0,2,1,4,5]
2、二维数组
x = np.array([[1,5,4],[-1,6,9]])
# [[ 1 5 4]
# [-1 6 9]]
沿着行向下(每列)的元素进行排序
np.argsort(x,axis=0)
# array([[1, 0, 0],
# [0, 1, 1]], dtype=int64)
沿着列向右(每行)的元素进行排序
np.argsort(x,axis=1)
# array([[0, 2, 1],
# [0, 1, 2]], dtype=int64)
参考:Python基本函数:【numpy】np.argsort()函数详细解释运算过程_当一颗恒星的博客-CSDN博客_np.argsort
边栏推荐
猜你喜欢
![[Image processing] Image skeleton extraction based on central axis transformation with matlab code](/img/56/cfef9389291fa39612d55a07e1dbb3.png)
[Image processing] Image skeleton extraction based on central axis transformation with matlab code

MySQL(3)

Redis学习

应用实践 | Apache Doris 在百度智能云计费账单系统的应用实践

cmd (command line) to operate or connect to the mysql database, and to create databases and tables

Error: npm ERR code EPERM

429. N 叉树的层序遍历(两种解法)

Mysql8.+学习笔记

MySQL Soul 16 Questions, how many questions can you last?

手把手教你彻底卸载MySQL
随机推荐
JVM之GC 调优工具 Arthas 实战使用(二)
ClickHouse data insert, update and delete operations SQL
CISP-PTE真题演示
Falling ants (Peking University entrance exam questions)
pwn-ROP
MySQL 用户授权
IDEA的database使用教程(使用mysql数据库)
nacos-2.0.3启动报错出现no datasource set的坑
[Koltin Flow (2)] The end operator of the Flow operator
【飞控开发基础教程9】疯壳·开源编队无人机-PWM(电机控制)
MySQL fuzzy query performance optimization
Introduction to Oracle Patch System and Opatch Tool
成绩排序(华中科技大学考研机试题)(DAY 87)
[Mysql] DATEDIFF函数
个人博客系统(附源码)
Teach you to completely uninstall MySQL
机器学习—梯度下降Gradient Descent Optimization—c语言实现
最新版MySQL 8.0 的下载与安装(详细教程)
PyCharm usage tutorial (more detailed, picture + text)
G巴士计数(Google Kickstart2014 Round D Problem B)(DAY 89)