当前位置:网站首页>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 .
边栏推荐
- JS event introduction
- Docker-compose安装mysql
- [MySQL] historical cumulative de duplication of multiple indicators
- Log4j dynamic loading configuration file
- IT硬件故障的主要原因和预防的最佳实践
- SDRAM Controller Design (two design methods of digital controller)
- Docker compose install MySQL
- SQL question brushing: find the last of all employees who have been assigned departments_ Name and first_ Name and Dept_ no
- Rewriting method set
- 【HCIP】MPLS 基础
猜你喜欢
随机推荐
[MySQL] string to int
Oozie工作调度
SQL injection of DVWA
BOM系列之window对象
J9数字论:什么因素决定NFT的价值?
新1688 API 接入说明
Naver three party login
Openpyxl cell center
Plato launched the LAAS protocol elephant swap, which allows users to earn premium income
Canal real-time parsing MySQL binlog data practice
Learning summary of time complexity and space complexity
Comprehensive upgrade, all you can imagine is here -- JD API interface
Digital currency of quantitative transactions - generate foot print factor data
AlphaFold揭示了蛋白质结构宇宙-从近100万个结构扩展到超过2亿个结构
代码生成器
TypeError: can only concatenate str (not “int“) to str
Google play APK uploads other international app stores
Flink Postgres CDC
[ManageEngine] help Harbin Engineering University realize integrated monitoring and management of network traffic
Third party login process of flask Weibo




![[leetcode sliding window problem]](/img/84/566d3805e52c358603694cdec69a13.png)




