OKPEDIA PYTHON VECTOR

How to calculate eigenvectors in Python

To calculate the eigenvectors on Python use the linalg.eig() function of the NumPy module.

linalg.eig(m)

The parameter m is an array.

The linalg.eig() function returns the eigenvalues and eigenvectors of the array.

A practical example

Import the numpy module.

import numpy as np

Define an array.

matrix = np.array([[3,1],[1,1]])

It is a square matrix

$$ matrix = \begin{pmatrix} 3 & 1 \\ 1 & 1 \end{pmatrix} $$

Calculate the eigenvalues and eigenvectors of the matrix by eig() function

eval, evec = np.linalg.eig(matrix)

The eigenvalues of the matrix are

>>> eval
array([3.41421356, 0.58578644])

The eigenvectors of the matrix are arranged in a column

>>> evec
array([[ 0.92387953, -0.38268343],
[ 0.38268343, 0.92387953]])

Transpose the eigenvector matrix

evec = np.transpose(evec)

Now the eigenvectors of the matrix are arranged in a row

array([[ 0.92387953, 0.38268343],
[-0.38268343, 0.92387953]])

Verify

Calculate the product between the matrix and the first eigenvector

>>> np.dot(matrix,evec[0])
array([3.15432203, 1.30656296])

Calculate the product between the first eigenvector and the first eigenvalue

>>> evec[0]*eval[0]
array([3.15432203, 1.30656296])

The result is the same

Calculate the product of the matrix and the second eigenvector

>>> np.dot(matrix,evec[1])
array([-0.22417076, 0.5411961 ])

Calculate the product of the first eigenvector and the second eigenvalue

>>> evec[1]*eval[1]
array([-0.22417076, 0.5411961 ])

The result is the same and the calculation is correct.

https://how.okpedia.org/en/python/how-to-calculate-eigenvectors-in-python


Report us an error or send a suggestion to improve this page


Python


FacebookTwitterLinkedinLinkedin