当前位置:网站首页>Numpy sort search count set
Numpy sort search count set
2022-04-23 20:16:00 【_ Carpediem】
import numpy as np
x=np.random.randint(1,10,10)
print(x)
[2 2 6 2 6 4 2 3 1 6]
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
[[8.68 2.14 3.02 1.86 7.85]
[1.76 5.14 6.52 4.49 7.2 ]
[8.16 7.3 2.34 2.37 7.31]
[1.58 9.48 0.14 9.29 6.6 ]
[9.18 4.98 9.1 0.7 7.94]]
y=np.sort(x)# Each row of elements is sorted by itself
print(y)
[[1.86 2.14 3.02 7.85 8.68]
[1.76 4.49 5.14 6.52 7.2 ]
[2.34 2.37 7.3 7.31 8.16]
[0.14 1.58 6.6 9.29 9.48]
[0.7 4.98 7.94 9.1 9.18]]
print(np.sort(x,axis=0))# Each column element is sorted separately
print(np.sort(x,axis=1))# Each row of elements is sorted separately
[[1.58 2.14 0.14 0.7 6.6 ]
[1.76 4.98 2.34 1.86 7.2 ]
[8.16 5.14 3.02 2.37 7.31]
[8.68 7.3 6.52 4.49 7.85]
[9.18 9.48 9.1 9.29 7.94]]
[[1.86 2.14 3.02 7.85 8.68]
[1.76 4.49 5.14 6.52 7.2 ]
[2.34 2.37 7.3 7.31 8.16]
[0.14 1.58 6.6 9.29 9.48]
[0.7 4.98 7.94 9.1 9.18]]
x = np.random.randint(0, 10, 10)
print(x)
y=np.argsort(x)# Return sort ( Ascending ) Index subscript of the array after
z=np.argsort(-x)# Descending
print(y)
print(x[y])
print(z)
print(x[z])
[2 2 2 2 0 6 1 7 8 5]
[4 6 0 1 2 3 9 5 7 8]
[0 1 2 2 2 2 5 6 7 8]
[8 7 5 9 0 1 2 3 6 4]
[8 7 6 5 2 2 2 2 1 0]
x=np.array([[1,2],[0,2]])
y=np.nonzero(x)
print(y,type(y))
z=np.transpose(y)
print(z)
(array([0, 0, 1], dtype=int64), array([0, 1, 1], dtype=int64)) <class 'tuple'>
[[0 0]
[0 1]
[1 1]]
x = np.array([1, 5, 1, 4, 3, 4, 4])
y = np.array([9, 4, 0, 4, 0, 2, 1])
a = np.lexsort([x])
b = np.lexsort([-y]) # Descending
print(a)
print(b)
[0 2 4 3 5 6 1]
[0 1 3 5 6 2 4]
aggregate :
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
Find the unique elements of an array.
return_index=True Returns the position of the new list element in the old list .
return_inverse=True Returns the position of the old list element in the new list .
return_counts=True Indicates the number of times the new list element appears in the old list .
numpy.in1d(ar1, ar2, assume_unique=False, invert=False)
Test whether each element of a 1-D array is also present in a second array.
Whether the preceding array is included in the following array , Returns a Boolean value . The returned value is for the array of the first parameter , So the dimension is consistent with the first parameter , Boolean values also correspond to the element positions of the array one by one .
Find the intersection of two sets :
numpy.intersect1d(ar1, ar2, assume_unique=False, return_indices=False)
Find the intersection of two arrays.
Return the sorted, unique values that are in both of the input arrays.
// Find the uniqueness of two arrays + Find the intersection + Sorting function .
import numpy as np
from functools import reduce
x = np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
print(x) # [1 3]
x = np.array([1, 1, 2, 3, 4])
y = np.array([2, 1, 4, 6])
xy, x_ind, y_ind = np.intersect1d(x, y, return_indices=True)
print(x_ind) # [0 2 4]
print(y_ind) # [1 0 2]
print(xy) # [1 2 4]
print(x[x_ind]) # [1 2 4]
print(y[y_ind]) # [1 2 4]
x = reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
print(x) # [3]
Find the union of two sets :
numpy.union1d(ar1, ar2) Find the union of two arrays.
Return the unique, sorted array of values that are in either of the two input arrays.
// Calculate the union of two sets , Uniqueness and sorting .
import numpy as np
from functools import reduce
x = np.union1d([-1, 0, 1], [-2, 0, 2])
print(x) # [-2 -1 0 1 2]
x = reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
print(x) # [1 2 3 4 6]
''' functools.reduce(function, iterable[, initializer]) Two parameters of function Apply cumulatively from left to right to iterable The entry of , To reduce the iteratable object to a single value . for example ,reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) It's calculation ((((1+2)+3)+4)+5) Value . Parameters on the left x Is the accumulated value, and the parameter on the right y It's from iterable The update value of . If there are options initializer, It will be placed before the entry of the iteratable object involved in the calculation , It is used as the default value when the iteratable object is empty . If not given initializer also iterable Contains only one entry , The first item... Will be returned . Roughly equivalent to : def reduce(function, iterable, initializer=None): it = iter(iterable) if initializer is None: value = next(it) else: value = initializer for element in it: value = function(value, element) return value '''
Find the difference set of two sets :
numpy.setdiff1d(ar1, ar2, assume_unique=False)
Find the set difference of two arrays.
Return the unique values in ar1 that are not in ar2.
// The difference in the set , That is, the element exists in the first function and does not exist in the second function .
import numpy as np
a = np.array([1, 2, 3, 2, 4, 1])
b = np.array([3, 4, 5, 6])
x = np.setdiff1d(a, b)
print(x) # [1 2]
Find the XOR of two sets :
setxor1d(ar1, ar2, assume_unique=False)
Find the set exclusive-or of two arrays.
// The symmetry difference of a set , That is, the complement of the intersection of two sets . in short , Is a collection of elements that are owned by each of the two arrays .
import numpy as np
a = np.array([1, 2, 3, 2, 4, 1])
b = np.array([3, 4, 5, 6])
x = np.setxor1d(a, b)
print(x) # [1 2 5 6]
版权声明
本文为[_ Carpediem]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210553546768.html
边栏推荐
- 【2022】将3D目标检测看作序列预测-Point2Seq: Detecting 3D Objects as Sequences
- 还在用 ListView?使用 AnimatedList 让列表元素动起来
- Database query - course selection system
- SIGIR'22「微软」CTR估计:利用上下文信息促进特征表征学习
- [talkative cloud native] load balancing - the passenger flow of small restaurants has increased
- Cadence Orcad Capture 批量更改元件封装功能介绍图文教程及视频演示
- VeraCrypt文件硬盘加密使用教程
- 波场DAO新物种下场,USDD如何破局稳定币市场?
- Remote code execution in Win 11 using wpad / PAC and JScript 3
- NC basic usage 4
猜你喜欢
Leetcode dynamic planning training camp (1-5 days)
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
Understanding various team patterns in scrum patterns
DNS cloud school rising posture! Three advanced uses of authoritative DNS
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
如何在BNB鏈上創建BEP-20通證
网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
[target tracking] pedestrian attitude recognition based on frame difference method combined with Kalman filter, with matlab code
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
随机推荐
Mysql database backup scheme
STM32基础知识
PCA based geometric feature calculation of PCL point cloud processing (52)
[2022] regard 3D target detection as sequence prediction - point2seq: detecting 3D objects as sequences
使用 WPAD/PAC 和 JScript在win11中进行远程代码执行3
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
Local call feign interface message 404
R语言survival包coxph函数构建cox回归模型、ggrisk包ggrisk函数和two_scatter函数可视化Cox回归的风险评分图、解读风险评分图、基于LIRI数据集(基因数据集)
Devops integration - environment variables and building tools of Jenkins service
Redis cache penetration, cache breakdown, cache avalanche
Cadence Orcad Capture 批量更改元件封装功能介绍图文教程及视频演示
PHP reference manual string (7.2000 words)
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(六)
Sqoop imports tinyint type fields to boolean type
还在用 ListView?使用 AnimatedList 让列表元素动起来
aqs的学习
NC basic usage
程序设计语言基础(2)
How to do product innovation—— Exploration of product innovation methodology I