当前位置:网站首页>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
边栏推荐
- How to do product innovation—— Exploration of product innovation methodology I
- Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (VII)
- How about CICC fortune? Is it safe to open an account
- Lpc1768 optimization comparison of delay time and different levels
- 【数值预测案例】(3) LSTM 时间序列电量预测,附Tensorflow完整代码
- Redis的安装(CentOS7命令行安装)
- WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
- Remote code execution in Win 11 using wpad / PAC and JScript 1
- MySQL 进阶 锁 -- MySQL锁概述、MySQL锁的分类:全局锁(数据备份)、表级锁(表共享读锁、表独占写锁、元数据锁、意向锁)、行级锁(行锁、间隙锁、临键锁)
- NC basic usage 1
猜你喜欢

JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files

CVPR 2022 | QueryDet:使用级联稀疏query加速高分辨率下的小目标检测

Leetcode dynamic planning training camp (1-5 days)

【数值预测案例】(3) LSTM 时间序列电量预测,附Tensorflow完整代码

Openharmony open source developer growth plan, looking for new open source forces that change the world!

Computing the intersection of two planes in PCL point cloud processing (51)

DTMF dual tone multi frequency signal simulation demonstration system

Servlet learning notes

程序设计语言基础(2)

STM32基础知识
随机推荐
Redis的安装(CentOS7命令行安装)
Remote code execution in Win 11 using wpad / PAC and JScript 1
2022 - Data Warehouse - [time dimension table] - year, week and holiday
nc基础用法1
Design of library management database system
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
Kubernetes getting started to proficient - install openelb on kubernetes
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
Mysql database backup scheme
基于pytorch搭建GoogleNet神经网络用于花类识别
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
How to create bep-20 pass on BNB chain
PCL点云处理之基于PCA的几何形状特征计算(五十二)
php参考手册String(7.2千字)
. Ren -- the intimate artifact in the field of vertical Recruitment!
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(五)
Local call feign interface message 404
Five minutes to show you what JWT is
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
R语言使用timeROC包计算存在竞争风险情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、R语言使用timeROC包的plotAUCcurve函数可视化多时间生存资料的AUC曲线