Eigenvalues and eigenvectors: Linear algebra in Python <7>

linear algebra

The linear algebra that you will definitely learn at a science university is summarized in an easy-to-understand and logical manner. By the way, I implemented it in Python. Occasionally, it may be implemented in Julia. .. .. ・ Learn by running with Python! New Mathematics Textbook-Basic Knowledge Required for Machine Learning / Deep Learning- ・ World Standard MIT Textbook Strang Linear Algebra Introduction Understand linear algebra based on and implement it in python.

environment

・ Jupyter Notebook ・ Language: Python3, Julia 1.4.0

Eigenvalues and eigenvectors

Eigenvalues and eigenvectors are used in principal component analysis to summarize data with artificial intelligence.

Eigenvalues, eigenvectors

Consider the square matrix A. For this matrix A

A\vec{x}=λ\vec{x}

When meeting

λ is the eigenvalue of the matrix A\\
\vec{x}The eigenvectors of the matrix A\\

That is.

Intrinsic equation

Multiply the above equation by the identity matrix E, which does not affect the vector.

A\vec{x}=λE\vec{x}

Transfer and organize the right side

(A-λE)\vec{x} = \vec{0}

This represents a vector with all 0 *** elements. On the other hand, even if the inverse matrix is considered, it is zero. It is a vector. This equation is called *** eigen equation ***.

Solving eigenvalues and eigenvectors with eigenvalues

(A-λE)\vec{x} = \vec{0}

along,

A=\begin{pmatrix}3 & 1\\2 & 4\end{pmatrix}

As a concrete example. Then

det(A-λE)\vec{x} = 0\\
⇔
det(\begin{pmatrix}3 & 1\\2 & 4\end{pmatrix}-λ\begin{pmatrix}1 & 0\\0 & 1\end{pmatrix})\vec{x} = 0\\
⇔
det\begin{pmatrix}3-λ & 1\\2 & 4-λ\end{pmatrix} = 0\\
⇔(λ-2)(λ-5)= 0\\

This makes the *** eigenvalue 2 or 5 ***. Because the eigenvector also changes depending on the eigenvalue. Think about each of the cases 2 and 5.

\vec{x} = \begin{pmatrix}p\\q\end{pmatrix}

If you calculate with λ = 2,

det(A-λE)\vec{x} = 0\\
\begin{align}
det(A-λE)\vec{x}&=
det(A-2E)\begin{pmatrix}p\\q\end{pmatrix} \\
&=\begin{pmatrix}1 & 1\\2 & 2\end{pmatrix}\begin{pmatrix}p\\q\end{pmatrix}\\
&=\begin{pmatrix}p+q\\2p+2q\end{pmatrix} \\
&= \vec{0} 
\end{align}

From this, it can be said that p + q = 0, so if you put an arbitrary real number t, the x vector will be

\vec{x}=\begin{pmatrix}t\\-t\end{pmatrix}

And the eigenvectors are obtained.

When λ = 5,

\vec{x}=\begin{pmatrix}t\\2t\end{pmatrix}

Will be.

program

Program to find eigenvalues and eigenvectors

python

7pythoneigenvaluevector


import numpy as np

A = np.array([[3, 1],[2, 4]])

ev = np.linalg.eig(A)
print(ev[0])
print(ev[1])
[2. 5.]
[[-0.70710678 -0.4472136 ]
 [ 0.70710678 -0.89442719]]

Here we are using the linag.eig function. This is because the eigenvalues are in English and are called *** eigenvalue ***.

Let's make a principle program. This time, the eigenvalues are calculated.

python (principle)

7pythoneigenvaluevector2


import numpy as np
import sympy

#A = [[a, b],
#     [c, d]]
a = int(input())
b = int(input())
c = int(input())
d = int(input())
#=>3
#=>1
#=>2
#=>4

x = sympy.Symbol('x')
eigenequa = x**2 - (a + d)*x + (a * d) - (b * c)```
print(eigenequa)
 factorization = sympy.factor(eigenequa)
 print(factorization)
solve = sympy.solve(eigenequa)
print(solve)
=>x**2 - 7*x + 10
=>[2, 5]

julia is so easy that I'll put it on.

julia

7juliaigenvaluevector


using LinearAlgebra
F = eigen([3 1; 2 4;])

=>Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}
=>values:
=>2-element Array{Float64,1}:
=> 2.0
=> 5.0
=>vectors:
=>2×2 Array{Float64,2}:
=> -0.707107  -0.447214
=>  0.707107  -0.894427

Only 2 lines! Wow...

That's all for today

Recommended Posts

Eigenvalues and eigenvectors: Linear algebra in Python <7>
Linear Independence and Basis: Linear Algebra in Python <6>
Identity matrix and inverse matrix: Linear algebra in Python <4>
Inner product and vector: Linear algebra in Python <2>
Matrix Calculations and Linear Equations: Linear Algebra in Python <3>
First Computational Physics: Quantum mechanics and linear algebra in python.
Basic Linear Algebra Learned in Python (Part 1)
Linear Algebra for Programming Chapter 4 (Eigenvalues, Eigenvectors)
Introduction to Vectors: Linear Algebra in Python <1>
Find eigenvalues and eigenvectors
Linear search in Python
"Linear regression" and "Probabilistic version of linear regression" in Python "Bayesian linear regression"
Find the Hermitian matrix and its eigenvalues in Python
Stack and Queue in Python
Introduction to Linear Algebra in Python: A = LU Decomposition
Unittest and CI in Python
Overview of generalized linear models and implementation in Python
Online linear regression in Python
Capture linear algebra images in python (transpose, inverse matrix, matrix product)
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Solving simultaneous linear equations in Python (sweeping method and fractional representation)
List of Linear Programming (LP) solvers and modelers available in Python
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Linear regression in Python (statmodels, scikit-learn, PyMC3)
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Implementation module "deque" in queue and Python
Line graphs and scale lines in python
Online Linear Regression in Python (Robust Estimate)
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Check and receive Serial port in Python (Port check)
Search and play YouTube videos in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
Dealing with "years and months" in Python
Read and write JSON files in Python
Easily graph data in shell and Python