# Matrix eigenvector and eigenvalue

2020-11-13 18:56:59

Recent learning LDA, We need to calculate eigenvalues and eigenvectors , I learned a new wave of

Eigenvalues are calculated using Python Relatively simple , Import required numpy Of linalg Calculation .
linalg yes linear algebra The abbreviation of .

First import Numpy

``import numpy as np``

Randomly generate a matrix A

``A = np.random.rand(4, 4)``
``````A
array([[0.14978175, 0.60689778, 0.02583363, 0.46816227],
[0.28508934, 0.74476942, 0.48711273, 0.75551799],
[0.54103663, 0.57551838, 0.16542061, 0.06687122],
[0.99511415, 0.07225251, 0.67671701, 0.80672535]])``````

We use lambda Represents the characteristic value , Use W Represent eigenvectors

``lambda,W = np.linalg.eig(A)``

We notice that both eigenvalues and eigenvariables contain complex parts

``````Lambda
array([ 1.90910151+0.j        , -0.16902995+0.48364592j,
-0.16902995-0.48364592j,  0.29565552+0.j        ])``````
``````W
array([[-0.37740651+0.j        , -0.0403403 +0.46883807j,
-0.0403403 -0.46883807j,  0.17922515+0.j        ],
[-0.62172554+0.j        , -0.30567755+0.1341112j ,
-0.30567755-0.1341112j , -0.39087808+0.j        ],
[-0.34506145+0.j        ,  0.61011431+0.j        ,
0.61011431-0.j        , -0.67479126+0.j        ],
[-0.59325734+0.j        , -0.09427735-0.5348002j ,
-0.09427735+0.5348002j ,  0.59979116+0.j        ]])``````

set up Sigma Is a diagonal matrix , The values on the diagonal are eigenvalues

``````Sigma = np.array(np.identity(4),dtype=complex)
for i in range(4):
Sigma[i,i] = Lambda[i]``````

here `np.identity(4)` Denotes the production of an identity matrix ,`dtype=complex` Express Sigma The matrix contains a complex part .

``````Sigma
array([[ 1.90910151+0.j        ,  0.        +0.j        ,
0.        +0.j        ,  0.        +0.j        ],
[ 0.        +0.j        , -0.16902995+0.48364592j,
0.        +0.j        ,  0.        +0.j        ],
[ 0.        +0.j        ,  0.        +0.j        ,
-0.16902995-0.48364592j,  0.        +0.j        ],
[ 0.        +0.j        ,  0.        +0.j        ,
0.        +0.j        ,  0.29565552+0.j        ]])
``\
``````

The original code of the mathematical formula involved in the test is as follows ：

This is an in line formula ：\$P = frac{C_a^k cdot C_b^{n-k}}{C_{a+b}^n}\$

These are two single line formulas ：
\$\$P = frac{C_a^k cdot C_b^{n-k}}{C_{a+b}^n}\$\$

\$\$ P = frac{C_a^k cdot C_b^{n-k}}{C_{a+b}^n} \$\$