0
0
SciPydata~5 mins

Eigenvalues and eigenvectors (eig) in SciPy

Choose your learning style9 modes available
Introduction

Eigenvalues and eigenvectors help us understand important properties of a matrix. They show directions that don't change when the matrix acts on them, and how much they stretch or shrink.

To find main directions of data in Principal Component Analysis (PCA).
To analyze stability in systems like population growth or mechanical vibrations.
To simplify complex matrix operations by working with eigenvalues.
To understand transformations in graphics and physics.
To solve systems of linear differential equations.
Syntax
SciPy
from scipy.linalg import eig

values, vectors = eig(matrix)

matrix must be a square 2D array (same number of rows and columns).

values are the eigenvalues, and vectors are the eigenvectors as columns.

Examples
Simple diagonal matrix where eigenvalues are the diagonal elements.
SciPy
import numpy as np
from scipy.linalg import eig

A = np.array([[2, 0], [0, 3]])
values, vectors = eig(A)
Matrix representing rotation; eigenvalues are complex numbers.
SciPy
import numpy as np
from scipy.linalg import eig

B = np.array([[0, -1], [1, 0]])
values, vectors = eig(B)
Sample Program

This program calculates and prints the eigenvalues and eigenvectors of matrix M.

SciPy
import numpy as np
from scipy.linalg import eig

# Define a 2x2 matrix
M = np.array([[4, 2], [1, 3]])

# Calculate eigenvalues and eigenvectors
values, vectors = eig(M)

# Print eigenvalues
print('Eigenvalues:')
print(values)

# Print eigenvectors
print('\nEigenvectors (each column is one):')
print(vectors)
OutputSuccess
Important Notes

Eigenvalues can be complex numbers, even if the matrix has only real numbers.

Eigenvectors are normalized by default in scipy.linalg.eig.

Order of eigenvalues and eigenvectors corresponds; the first eigenvalue matches the first eigenvector column.

Summary

Eigenvalues show how much vectors stretch or shrink under a matrix.

Eigenvectors show directions that stay the same when the matrix acts on them.

Use scipy.linalg.eig to find both easily for square matrices.