当前位置:网站首页>numpy. Where() usage and np.argsort() usage
numpy. Where() usage and np.argsort() usage
2022-07-29 01:35:00 【51CTO】
numpy.where() There are two uses :
1. np.where(condition, x, y)
Meet the conditions (condition), Output x, Not satisfied with output y.
If it's a one-dimensional array , amount to [xv if c else yv for (c,xv,yv) in zip(condition,x,y)]
The condition of the above example is [[True,False], [True,False]], Respectively corresponding to the four values of the final output result . The first value is from [1,9] Middle selection , Because the condition is True, So choose 1. The second value is from [2,8] Middle selection , Because the condition is False, So choose 8, And so on . You can take another example of a similar problem :
2. np.where(condition)
Only the conditions (condition), No, x and y, Then the output meets the condition ( It's not 0) The coordinates of the elements ( Equivalent to numpy.nonzero). The coordinates here are expressed in tuple Given in the form of , How many dimensions does the original array usually have , Output tuple It contains several arrays , Corresponding to the coordinates of each dimension of the qualified element .
In the above example condition [[0,1],[1,0]] The true value of is two 1, Their first dimensional coordinates are [0,1], The second coordinate is [1,0] .
Let's take a look at a complicated example :
therefore np.where The corresponding coordinates of each element will be output , Because the original array has three dimensions , therefore tuple There are three arrays in .
One thing to note is that , The input cannot be directly list, Need to change to array Or for array Talent . such as range(10) and np.arange(10) The latter returns an array , Use np.where In order to achieve the effect .
np.argsort() Usage of
argsort(a)# obtain a An array arranged from small to large
argsort(-a)# obtain a Array arranged from large to small
argmin(a)# obtain a Minimum subscript
argmax(a)# obtain a The maximum subscript
function : The matrix a according to axis Sort , And return the sorted subscript
Parameters : a: Input matrix , axis: Dimensions to be sorted
Return value : Output the sorted subscript
( One dimensional array )
You can find ,argsort() Yes, it will X After the elements in are sorted from small to large , Extract the corresponding index index, Then output to y
Such as x[3]=-1 Minimum ,x[5]=9 Maximum
So take the array x The minimum value of can be written as :
Or use argmin() function
Array x The maximum of , It's written in :
Or use argmax() function , No more details
Output sorted array
( Two dimensional array )
Go right along the column ( Each row ) The elements are sorted
author : Your Rego
The copyright of this article belongs to the author , Welcome to reprint , But without the author's consent, the original link must be given on the article page , Otherwise, the right to pursue legal responsibility is reserved .
边栏推荐
- Log4j dynamic loading configuration file
- Embedded sharing collection 23
- Flink SQL Hudi 实战
- API stability guarantee of Prometheus
- Ruiji takeout project actual battle day01
- Lombook User Guide
- SQL question brushing: find the last of all employees who have been assigned departments_ Name and first_ Name and Dept_ no
- 北京护照西班牙语翻译推荐
- Synchronized关键字详解
- Rewriting method set
猜你喜欢

mysql 创建索引的三种方式

SQL injection of DVWA

Oozie工作调度

SQL question brushing: find the employee number EMP with more than 15 salary records_ No and its corresponding recording times t

Redis installation, cluster deployment and common tuning

SiC功率半导体产业高峰论坛成功举办

Redis is installed on Linux

Canal real-time parsing MySQL binlog data practice

body中基本标签

DocuWare 移动劳动力解决方案可帮助您构建新的生产力模式:随时随地、任何设备
随机推荐
New 1688 API access instructions
Self-attention neural architecture search for semantic image segmentation
如何选择专业、安全、高性能的远程控制软件
Openpyxl border
PlatoFarm社区生态福音,用户可借助Elephant Swap获得溢价收益
Canal real-time parsing MySQL binlog data practice
MySQL time is formatted by hour_ MySQL time format, MySQL statement queried by time period [easy to understand]
Third party login process of flask Weibo
mysql 创建索引的三种方式
SQL question brushing: find the current salary details and department number Dept_ no
Flask reports an error: pymysq1.err OperationalError:(1054, “Unknown column ‘None‘ in ‘field list‘“)
Cross modal alignment 20220728
新生代公链再攻「不可能三角」
Linux redis source code installation
matplotlib中文问题
DocuWare 移动劳动力解决方案可帮助您构建新的生产力模式:随时随地、任何设备
JS event introduction
J9数字论:什么因素决定NFT的价值?
Test / development programmers rely on technology to survive the midlife crisis? Improve your own value
uniapp movable-view表格缩放过程想保持容器高度不变的解决办法