当前位置:网站首页>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
边栏推荐
- 【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码
- JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
- 基于pytorch搭建GoogleNet神经网络用于花类识别
- 山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(六)
- SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
- R language ggplot2 visual facet_wrap, and use the lineheight parameter to customize the height of the facet icon tab (gray label bar)
- Design of library management database system
- Understanding various team patterns in scrum patterns
- NC basic usage 4
- Compact CUDA tutorial - CUDA driver API
猜你喜欢
Building googlenet neural network based on pytorch for flower recognition
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
Software College of Shandong University Project Training - Innovation Training - network security shooting range experimental platform (8)
DNS cloud school rising posture! Three advanced uses of authoritative DNS
php参考手册String(7.2千字)
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
aqs的学习
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
随机推荐
Electron入门教程3 ——进程通信
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 (V)
Record: call mapper to report null pointer Foreach > the usage of not removing repetition;
VeraCrypt文件硬盘加密使用教程
Database query - course selection system
Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?
uIP1. 0 actively sent problem understanding
[text classification cases] (4) RNN and LSTM film evaluation Tendency Classification, with tensorflow complete code attached
Notes of Tang Shu's grammar class in postgraduate entrance examination English
SRS deployment
DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
The market share of the financial industry exceeds 50%, and zdns has built a solid foundation for the financial technology network
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (6)
NC basic usage 4
Fundamentals of programming language (2)
[numerical prediction case] (3) LSTM time series electricity quantity prediction, with tensorflow complete code attached
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
R语言使用timeROC包计算无竞争风险情况下的生存资料多时间AUC值、使用confint函数计算无竞争风险情况下的生存资料多时间AUC指标的置信区间值
R语言survival包coxph函数构建cox回归模型、ggrisk包ggrisk函数和two_scatter函数可视化Cox回归的风险评分图、解读风险评分图、基于LIRI数据集(基因数据集)