Data Analysis Learning Notes (IV.)--NumPy: Linear algebra

Source: Internet
Author: User
Common LINALG functions
function Description
Diag Returns the diagonal (or non-diagonal) elements of a matrix in the form of a one-dimensional array, or converts a one-dimensional array to a matrix (non-diagonal element 0)
Dot Standard matrix multiplication
Trace Calculates the and of the diagonal elements
Det Determinant of a computed matrix
Eigvals Calculate the eigenvalues of a matrix
Eig Calculation of eigenvalues and eigenvectors of matrices
Inv Calculating the inverse of a square matrix
Pinv Moore-penrose pseudo-inverse of computational matrices
Qr Compute QR Decomposition
Svd Compute singular value decomposition (SVD)
Solve Solving a linear equation set of Ax=b, where A is a matrix
Lstsq Calculating the least squares solution of ax=b
Diag
# diag
x = Np.arange (9). Reshape ((3,3))
print (x)
' ' [
[0 1 2]
 [3 4 5]
 [6 7 8]] '
# take the data on the diagonal C7/>print (Np.diag (x))
' [0 4 8] '
# k>0, above the main diagonal
print (Np.diag (x, k=1))
' [1 5] ' '
# k <0, under the main diagonal
print (Np.diag (x, k=-1))
' [3 7] '
# Create matrix
print (Np.diag (Np.diag (x)) by the data array on the diagonal ' [
[0 0 0]
 [0 4 0]
 [0 0 8]]
 '
Dot: Matrix multiplication
# dot
A = np.arange (9). Reshape ((3,3))
print (A)
' [[0 1 2]
 [3 4 5]
 [6 7 8]] ' '
B = NP . Arange (6). Reshape ((3,2))
print (b)
' [[0 1]
 [2 3]
 [4 5]] '
print (A.dot (b))
Print (Np.dot (a,b)) ' [[] [[A] [[]] '
 
Trace: And on the diagonal
A = Np.arange (9). Reshape ((3,3))
print (a)
' [[0 1 2]
 [3 4 5]
 [6 7 8]]
 '
print (A.trace ( )
Print (Np.trace (A))
' ' 12 '
Det: determinant
x = Np.mat (' 3 4;2 2 ')
print (x)
' ' [[3 4]
 [2 2]] '
print (Np.linalg.det (x)) '
-2 ' "
Eigvals, Eig: Calculating eigenvalues and eigenvectors

Note: Eigenvalue (eigenvalue) is the equation ax = ax root, is a scalar. Where A is a two-dimensional matrix, and X is a one-dimensional vector. Eigenvector (eigenvector) is a vector of eigenvalues

The function Eigvals function can compute the eigenvalues of a matrix, whereas the EIG function can return a tuple that contains eigenvalues and corresponding eigenvectors.

C = Np.mat (' 3-2;1 0 ')
# calls the Eigvals function to solve the eigenvalue
E0 = Np.linalg.eigvals (c)
print (E0)
' [2. 1.] '
# using the EIG function to solve eigenvalues and eigenvectors, the first column is the eigenvalue, the second column is the eigenvector
e1,e2 = Np.linalg.eig (C)
print (E1)
' [2. 1.] '
print (E2) '
[[0.89442719 0.70710678] [
 0.4472136  0.70710678]]
 '
# Check Results
for I in range (Len (E1)):
    print ("Left:", Np.dot (C, e2[:, i))
    print ("Right:, E1[i] * e2[:, I])
" C18/>left: [[1.78885438]
 [0.89442719]] right
: [[1.78885438]
 [0.89442719]] left
: [[0.70710678]
 [0.70710678]]
Right: [[0.70710678]
 [0.70710678]]
 '
INV: The inverse of a square matrix
A = Np.mat (' 0 1 2;1 0 3;4-3 8 ')
print (a)
' '
[[0  1 2  ]
 [1 0 3]
 [4-3  8]]
 ' '
# Use the INV function to compute the inverse matrix
inv = NP.LINALG.INV (A)
print (INV)
'
[ -4.5  7.  -1.5]
 [ -2.   4.  -1.]
 [1.5-2.   0.5]] '
# Check Results
print (INV * A) '
[1. 0.0.]
 [0.1. 0.]
 [0. 0.1.]]
 ""
PINV: Generalized inverse matrix
A = Np.mat (' 1 2;4 3 ')
print (a)
' '
[[1 2]
 [4 3]]
 '
PSEUDOINV = NP.LINALG.PINV (a)
Print (PSEUDOINV)
'
[[ -0.6  0.4] [
 0.8-0.2]]
 '
SVD: singular decomposition

SVD (Singular value decomposition, singular value decomposition) is a factorization operation that decomposes a matrix into the product of 3 matrices
SVD can perform singular value decomposition of a matrix, which returns 3 matrices –u, Sigma, and V, where U and V are orthogonal matrices, and Sigma contains the singular value of the input matrix.

A = Np.mat (' 4 14;8 7-2 ')
print (a)
'
[[4]
 [8  7-2]] '
# Call the SVD function decomposition matrix
U,sigma , V = NP.LINALG.SVD (A, Full_matrices=false)
print (' u:{}{}sigma:{}{}v:{}{} '. Format (U, ' \ n ', Sigma, ' \ n ', V, ' \ n '))
'''
u:[[-0.9486833  -0.31622777]
 [ -0.31622777  0.9486833]]]
sigma:[18.97366596  9.48683298]
v:[[-0.33333333-0.66666667-0.66666667]
 [0.66666667  0.33333333-0.66666667]]
 '
# Check Results
Print (U * NP.DIAG (SIGMA) * V)
'
[4. One.]
 [8.  7. -2.]]
 "
Solve: Solving systems of linear equations

Solve can solve a linear equation group, such as Ax = B, where A is a matrix, B is a one-dimensional or two-dimensional array, and X is an unknown variable

B = Np.mat (' 1-2 1;0 2-8;-4 5 9 ')
print (B)
'
[[1-2  1]
 [0  2-8] [
 -4 5 9  ]]
' b = Np.array ([0, 2, 2])
x = Np.linalg.solve (b, b)
print (x)
"" [37.  5.] '
# Check Results
print (B.dot (x))
' [0. 2.2.]] ""
and b equal ""

QR decomposition
For the column full rank matrix A of MXN, there must be:

           am*n= Qm*n Rn*n

Among them, Qt Q=i (that is, Q is an orthogonal matrix), R is a nonsingular upper triangular matrix (that is, the elements under the diagonal of the matrix R are all 0).

The process of splitting a into such a matrix Q and R is the QR decomposition.

Where the diagonal element of R is required to be positive, the decomposition is unique.

QR decomposition can be used to solve the eigenvalue of matrix A, the inverse of a and so on.

# qr decomposition A = Np.array ([[[1,2,3,4,0],[-1,3,np.sqrt (2), 3,0],[-2,2,NP.E,NP.PI,0],[-NP.SQRT (10),          2,-3,7,0],[0,2,7,5/2,0]],dtype=float) print (A) ' ' [1.          2.3.        4.0.          ] [-1.          3.1.41421356 3.          0.] [-2.        2.2.71828183 3.14159265 0.         ] [-3.16227766 2.          -3.        7.0.          ] [0.          2.7.        2.5 0.          ]] ' A = Np.matrix (a,dtype=float) q,r = NP.LINALG.QR (a) # Verify print (Q*r) ' [1.          2.3.        4.0.          ] [-1.          3.1.41421356 3.          0.] [-2.        2.2.71828183 3.14159265 0.         ] [-3.16227766 2.          -3.        7.0.          ] [0.          2.7.        2.5 0. ]]
 '''

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.