当前位置:网站首页>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
边栏推荐
- 使用DataEase开源工具制作一个高质量的数据大屏
- It is enough for MySQL to have this article (37k words, just like Bojun!!!)
- MySQL database basics (a systematic introduction)
- cmd (command line) to operate or connect to the mysql database, and to create databases and tables
- Different usage scenarios of subqueries as retrieval tables and the question of whether to add aliases
- 从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
- JVM之GC 调优工具 Arthas 实战使用(二)
- Seata exception: endpoint format should like ip:port
- 微信小程序开发学习
- [Mysql] DATEDIFF function
猜你喜欢

手把手教你设计一个CSDN系统

Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案

个人博客系统(附源码)

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

St. Regis Takeaway Project: New dishes and dishes paged query

IDEA的database使用教程(使用mysql数据库)

MySQL(4)

mysql 时间字段默认设置为当前时间

分布式事务之 Seata框架的原理和实战使用(三)

cmd(命令行)操作或连接mysql数据库,以及创建数据库与表
随机推荐
【Koltin Flow(二)】Flow操作符之末端操作符
839. 模拟堆
分布式事务之 Seata框架的原理和实战使用(三)
2022 Pengcheng Cup web
Navicat cannot connect to mysql super detailed processing method
This dependency was not found:
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
CISP-PTE Zhenti Demonstration
MySQL索引常见面试题(2022版)
Record Breaker (Google Kickstart2020 Round D Problem A)
cnpm installation steps
“tensorflow.keras.preprocessing“ has no attribute “image_dataset_from_directory“
Different usage scenarios of subqueries as retrieval tables and the question of whether to add aliases
[Mysql] CONVERT函数
4、nerf(pytorch)
JVM 类加载机制 超详细学习笔记(三)
1475. 商品折扣后的最终价格
从字节码角度带你彻底理解异常中catch,return和finally,再也不用死记硬背了
[GO Language Basics] 1. Why do I want to learn Golang and the popularization of GO language entry
cookie和session区别