当前位置:网站首页>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

Array

np.ones(): Create an array

np.array(): Convert to array type

reshape(): Reorganize array

The array performs four operations  

ix_(): Slice the array

  Fusion between arrays :hstack() and vstack()

save() Data access and load() load

ravel(): Is an array programming one-dimensional array

sort(): Sort by value

argmax(): Get the index of the maximum value  

argmin(): Get the minimum index  

matrix

mat(): Convert to matrix

eye(): Create identity matrix

bmat(): Composite matrix

The transpose of the matrix , Conjugate transpose of matrix  

A matrix can perform four operations  

Inverse matrix

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

原网站

版权声明
本文为[A little monster who loves playing badminton]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220448249227.html