当前位置:网站首页>Common knowledge arrangement of numpy database
Common knowledge arrangement of numpy database
2022-06-22 04:51:00 【A little monster who loves playing badminton】
Catalog
np.array(): Convert to array type
The array performs four operations
Fusion between arrays :hstack() and vstack()
save() Data access and load() load
ravel(): Is an array programming one-dimensional array
argmax(): Get the index of the maximum value
argmin(): Get the minimum index
The transpose of the matrix , Conjugate transpose of matrix
A matrix can perform four operations
Solve equations : Where the matrix is the coefficient matrix , Resolve to array
Find eigenvalues and eigenvectors
Find the singular value decomposition of the matrix
Find the value of matrix determinant
Array
np.ones(): Create an array
>>> import numpy as np
>>> z1=np.ones((3,3))# Create an array
>>> print(z1)
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
<class 'numpy.ndarray'>np.array(): Convert to array type
>>> d1=[1,2,3,4,5,6,7,8,9]
>>> d11=np.array(d1)# Convert to array type
>>> s11=d11.shape# View size
>>> print(type(d11))
<class 'numpy.ndarray'>
>>> print(d11)
[1 2 3 4 5 6 7 8 9]
>>> print(s11)
(9,)reshape(): Reorganize array
To learn more about the lesson view Python——numpy In the library reshape Usage of _ I am a little monster blog -CSDN Blog
The array performs four operations
>>> A=np.array([[1,2],[3,4]])
>>> B=np.array([[5,6],[7,8]])
>>> c1=A-B
>>> c2=A*B# Array can perform four operations
>>> E=np.exp(A)
>>> print(E)
[[ 2.71828183 7.3890561 ]
[20.08553692 54.59815003]]ix_(): Slice the array
>>> D=np.array([[1,2,3,4,],[5,6,99,6],[5,6,12,3],[7,8,5,32]])
>>> print(D)
>>> d1=D[np.ix_([1,2],[0,3])]
>>> d2=D[np.ix_(np.arange(3),[1,3])]
>>> d3=D[np.ix_(D[:,1]>5,[2])]
>>> print(d1,d2,d3,sep='\n')
[[ 1 2 3 4]
[ 5 6 99 6]
[ 5 6 12 3]
[ 7 8 5 32]]
[[5 6]
[5 3]]
[[2 4]
[6 6]
[6 3]]
[[99]
[12]
[ 5]]Fusion between arrays :hstack() and vstack()
>>> A=np.array([[1,2],[3,4]])
>>> B=np.array([[5,6],[7,8]])
>>> C1=np.hstack((A,B))# The same number of lines is required
>>> C2=np.vstack((A,B))# The number of columns is required to be the same
>>> print(C1,C2,sep='\n')
[[1 2 5 6]
[3 4 7 8]]
[[1 2]
[3 4]
[5 6]
[7 8]]save() Data access and load() load
# Access to data /// Use save(' file name ', Data name ) Store the data /// Use load(' file name . file type ')
>>> A=np.array([[1,2],[3,4]])
>>> np.save('data',A)
>>> Q=np.load('data.npy')
>>> print(Q)
[[1 2]
[3 4]]ravel(): Is an array programming one-dimensional array
>>> arr=np.arange(12)
>>> arr1=arr.reshape((3,4))
>>> arr2=arr1.ravel()
>>> print(arr,arr1,arr2,sep='\n')
[ 0 1 2 3 4 5 6 7 8 9 10 11]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[ 0 1 2 3 4 5 6 7 8 9 10 11]sort(): Sort by value
argmax(): Get the index of the maximum value
argmin(): Get the minimum index
>>> arrs=np.array([516,1,132,15,19,561,56,1616,16,16,65,13,651,16,12])
>>> arrs_1=np.sort(arrs)
>>> print(arrs_1)
[ 1 12 13 15 16 16 16 19 56 65 132 516 561 651
1616]
>>> arrs_2=arrs.reshape(3,5)
>>> print(arrs_2)
[[ 516 1 132 15 19]
[ 561 56 1616 16 16]
[ 65 13 651 16 12]]
>>> maxindex=np.argmax(arrs)# Get the index of the maximum value
>>> minindex=np.argmin(arrs)# Get the minimum index
>>> maxindex_1=np.argmax(arrs_2,axis=0)
>>> minindex_1=np.argmin(arrs_2,axis=1)
>>> print(maxindex,minindex,maxindex_1,minindex_1,sep='\n')
7
1
[1 1 1 1 0]
[1 3 4]matrix
mat(): Convert to matrix
>>> mat1=np.mat('1,5,9;,5,6,4;,6,8,7')# Convert to matrix
>>> mat2=np.mat(arrs_2)
>>> print(mat1,arrs_2,sep='\n')
[[1 5 9]
[5 6 4]
[6 8 7]]
[[ 516 1 132 15 19]
[ 561 56 1616 16 16]
[ 65 13 651 16 12]]eye(): Create identity matrix
bmat(): Composite matrix
The transpose of the matrix , Conjugate transpose of matrix
>>> arr1=np.eye(3)# Create identity matrix
>>> arr2=arr1*4
>>> arr3=arr1*6
>>> mat_s=np.bmat('arr1 arr2 arr3;arr1 arr3 arr2;arr3 arr1 arr2')
>>> print(mat_s,end='\n')
[[1. 0. 0. 4. 0. 0. 6. 0. 0.]
[0. 1. 0. 0. 4. 0. 0. 6. 0.]
[0. 0. 1. 0. 0. 4. 0. 0. 6.]
[1. 0. 0. 6. 0. 0. 4. 0. 0.]
[0. 1. 0. 0. 6. 0. 0. 4. 0.]
[0. 0. 1. 0. 0. 6. 0. 0. 4.]
[6. 0. 0. 1. 0. 0. 4. 0. 0.]
[0. 6. 0. 0. 1. 0. 0. 4. 0.]
[0. 0. 6. 0. 0. 1. 0. 0. 4.]]
>>> print(mat_s.T,end='\n')# Transposition
[[1. 0. 0. 1. 0. 0. 6. 0. 0.]
[0. 1. 0. 0. 1. 0. 0. 6. 0.]
[0. 0. 1. 0. 0. 1. 0. 0. 6.]
[4. 0. 0. 6. 0. 0. 1. 0. 0.]
[0. 4. 0. 0. 6. 0. 0. 1. 0.]
[0. 0. 4. 0. 0. 6. 0. 0. 1.]
[6. 0. 0. 4. 0. 0. 4. 0. 0.]
[0. 6. 0. 0. 4. 0. 0. 4. 0.]
[0. 0. 6. 0. 0. 4. 0. 0. 4.]]
>>> print(mat_s.H,end='\n')# Conjugate transpose
[[1. 0. 0. 1. 0. 0. 6. 0. 0.]
[0. 1. 0. 0. 1. 0. 0. 6. 0.]
[0. 0. 1. 0. 0. 1. 0. 0. 6.]
[4. 0. 0. 6. 0. 0. 1. 0. 0.]
[0. 4. 0. 0. 6. 0. 0. 1. 0.]
[0. 0. 4. 0. 0. 6. 0. 0. 1.]
[6. 0. 0. 4. 0. 0. 4. 0. 0.]
[0. 6. 0. 0. 4. 0. 0. 4. 0.]
[0. 0. 6. 0. 0. 4. 0. 0. 4.]]
>>> print(mat_s.I,end='\n')# Inverse matrix
[[-0.18181818 0. 0. 0.09090909 0. 0.
0.18181818 0. 0. ]
[ 0. -0.18181818 0. 0. 0.09090909 0.
0. 0.18181818 0. ]
[ 0. 0. -0.18181818 0. 0. 0.09090909
0. 0. 0.18181818]
[-0.18181818 0. 0. 0.29090909 0. 0.
-0.01818182 0. 0. ]
[ 0. -0.18181818 0. 0. 0.29090909 0.
0. -0.01818182 0. ]
[ 0. 0. -0.18181818 0. 0. 0.29090909
0. 0. -0.01818182]
[ 0.31818182 0. 0. -0.20909091 0. 0.
-0.01818182 0. 0. ]
[ 0. 0.31818182 0. 0. -0.20909091 0.
0. -0.01818182 0. ]
[ 0. 0. 0.31818182 0. 0. -0.20909091
0. 0. -0.01818182]]A matrix can perform four operations
# A matrix can perform four operations , According to the matrix algorithm in mathematics
>>> mat1=np.mat('1,2,3;4,5,6;5,6,8')
>>> mat2=mat1*3
>>> mat3=mat1*mat2
>>> mat4=np.multiply(mat1,mat2)# Point multiplication
>>> print(mat1,mat2,mat3,mat4,sep='\n')
[[1 2 3]
[4 5 6]
[5 6 8]]
[[ 3 6 9]
[12 15 18]
[15 18 24]]
[[ 72 90 117]
[162 207 270]
[207 264 345]]
[[ 3 12 27]
[ 48 75 108]
[ 75 108 192]]Inverse matrix
>>> mat=np.mat('1,1,1;1,2,3;,1,3,6')
>>> inverse=np.linalg.inv(mat)# Calculate the inverse matrix
>>> print(mat)
[[1 1 1]
[1 2 3]
[1 3 6]]
>>> print(inverse)
[[ 3. -3. 1.]
[-3. 5. -2.]
[ 1. -2. 1.]]Solve equations : Where the matrix is the coefficient matrix , Resolve to array
>>> # Solve equations
>>> A=np.mat('1,-1,1;2,1,0;,2,1,-1')
>>> b=np.array([4,3,-1])
>>> x=np.linalg.solve(A,b)
>>> print(x)
[1. 1. 4.]Find eigenvalues and eigenvectors
>>> # Solve eigenvalues and eigenvectors
>>> A=np.mat('1,0,2;0,3,0;2,0,1')
>>> print(np.linalg.eigvals(A))# The eigenvalue can be obtained directly
[ 3. -1. 3.]
>>> a_value,a_vector=np.linalg.eig(A)
>>> print(a_value,a_vector,sep='\n')
[ 3. -1. 3.]
[[ 0.70710678 -0.70710678 0. ]
[ 0. 0. 1. ]
[ 0.70710678 0.70710678 0. ]]Find the singular value decomposition of the matrix
>>> A=np.mat('4.0,11.0,14.0;8.0,7.0,-2.0')
>>> u,s,v=np.linalg.svd(A,full_matrices=False)# Find the singular value decomposition of a matrix
>>> print(u,s,v,sep='\n')
[[-0.9486833 -0.31622777]
[-0.31622777 0.9486833 ]]
[18.97366596 9.48683298]
[[-0.33333333 -0.66666667 -0.66666667]
[ 0.66666667 0.33333333 -0.66666667]]Find the value of matrix determinant
>>> A=np.mat('1,-1,1;2,1,0;,2,1,-1')# Solve the value of matrix determinant
>>> print(np.linalg.det(A))
-2.9999999999999996边栏推荐
- Daily question: the difference between ArrayList and LinkedList
- Debugging wechat built-in browser with chrome
- Network Interview eight part essay of daily knowledge points (TCP, startling group phenomenon, collaborative process)
- [fault diagnosis] using multithreading, the program does not report an error but does not run
- Web design and production final assignment report - minority music website
- Digital economy Wang Ning teaches you how to correctly choose short-term investment
- tinymce. Init() browser compatibility issue
- [scientific research notes] focal loss
- Some details
- [user guide] use of Tsinghua source
猜你喜欢

Qt保存QTextEdit内存至.txt文件中

Use putty to configure port mapping to realize the access of the external network to the server

Postman document parameterization

Graph calculation on nlive:nepal's graph calculation practice

Cloud native enthusiast weekly: Chaos mesh upgraded to CNCF incubation project

JUC - thread interrupt and thread waiting and wakeup (locksupport)

守护进程的流程

Arrangement of soft test subjects in the second half of 2022

Pull down refresh, push up load (easy to use, finally)

JUC - 线程中断与线程等待、唤醒(LockSupport)
随机推荐
软考2022年下半年考试科目安排
VirtualBox 6.1.34 发布
Browser -- a collection of common search operators -- use / instance
Target detection algorithm based on deep learning interview essential (rcnn~yolov5)
What is the value of the FC2 new domain name? How to resolve to a website?
cadence allegro 17. X conversion tool for downgrading to 16.6
网页设计与制作期末大作业报告——动画家宫崎骏
nest. Module dependency transitivity in JS practice
软件架构与模式:结构、组件、关系
Ora - 15063: ASM discovered an insufficient number of Disks for diskgroup Recovery - - -
Graph calculation on nlive:nepal's graph calculation practice
torch DDP Training
go学习(二、内建容器)
Knowledge points used in MVC project development (mvccontrib separates asp.net MVC project)
基于深度学习的目标检测算法面试必备(RCNN~YOLOv5)
【故障诊断】cv2.imwrite无法写入图片,但程序就是不报错
厉害了!淮北两企业获准使用地理标志产品专用标志
MySQL common SQL
MySQL notes
103. simple chat room 6: using socket communication