
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - Eigenvalues and Eigenvectors
In SciPy Eigenvalues and Eigenvectors are computed as part of solving problems involving linear transformations specifically for square matrices. These values and vectors provide insight into the properties of a matrix and are widely used in various scientific and engineering domains.
Before understanding how scipy is used to work with the Eigenvalues and Eigenvectors, let's understand about Eigenvalues and Eigenvectors as follows −
What are Eigenvalues and Eigenvectors?
Eigenvalues and Eigenvectors are mathematical concepts used in linear algebra to analyze transformations represented by square matrices. They provide insights into how a matrix transforms a vector.
What is Eigenvector (v)?
An Eigenvector of a matrix A is a non-zero vector that only changes in scale i.e., not direction when the matrix is applied to it. Mathematically for a square matrix A the Eigenvector is given as follows −
Av = v
Where, v is the Eigenvector, A is a square matrix of size n x n and is the corresponding eigenvalue.
What is Eigenvalue()?
The eigenvalue is the scalar by which the eigenvector v is stretched or compressed during the transformation. can be positive, negative or even complex.
Finding Eigenvalues and Eigenvectors
When we want to find the Eigenvalues and Eigenvectors of a square matrix A then we have to follow the below steps −
Step 1: Eigenvalues
The Eigenvalues of a sqaure matrix A are the scalars that satisfy the eqaution as follows −
det(A - I) = 0
Now we have to rewrite the Egienvalue equation Av = v as follows −
(A - I)v = 0
Where, I is the identity matrix of the same size as A and A - I is the new matrix obtained by subtracting times the identity matrix from A.
This equation has non-zero v only if det(A - I) = 0. This is called the characteristic equation and solving it gives the eigenvalues .
To solve the equation det(A - I) = 0 first we have to substitute into the natrix A - I. Next we have to compute the determinant of the resulting matrix and then solve the resulting polynomial equation for .
The roots of the polynomial are the eigenvalues of A.
Step 2: Eigenvectors
Once when we found the Eigenvalues then we can compute the Eigenvectors by solving the beloow equation −
(A - I)v = 0
This equation is a system of linear equations. It has infinitely many solutions because (AI) is singular i.e., its determinant is zero. Typically these solutions are expressed as a scaled version of one eigenvector.
Computing Eigenvalues and Eigenvectors
In SciPy Eigenvalues and Eigenvectors can be calculated using the scipy.linalg.eig() function which is part of the scipy.linalg module. This function is used for both general matrices and with specific optimizations, Hermitian or symmetric matrices.
Computing Eigenvalues and Eigenvectors of General matrix
Here is the example of computing the eigenvalues and eigenvectors of a general n x n matrix using scipy −
import numpy as np from scipy.linalg import eig # Define a 3x3 matrix A = np.array([[6, 2, 1], [2, 3, 1], [1, 1, 1]]) # Compute eigenvalues and eigenvectors eigenvalues, eigenvectors = eig(A) # Print results print("Matrix A:") print(A) print("\nEigenvalues:") print(eigenvalues) print("\nEigenvectors (columns):") print(eigenvectors)
Here is the output of computing the eigenvalues and eigenvectors using scipy library −
Matrix A: [[6 2 1] [2 3 1] [1 1 1]] Eigenvalues: [7.28799214+0.j 2.13307448+0.j 0.57893339+0.j] Eigenvectors (columns): [[ 0.86643225 0.49742503 -0.0431682 ] [ 0.45305757 -0.8195891 -0.35073145] [ 0.20984279 -0.28432735 0.9354806 ]]
Hermitian and Symmetric Matrices
Hermitian Matrix is a square matrix that is equal to its own conjugate transpose, which is defined as follows −
A = A^H
Where, AH is the conjugate transpose of A.
Symmetric Matrix is a special case of a Hermitian matrix where all elements are real so it satisfies and it is given as follows −
A = A^T
Where AT is the transpose of A.
Here's the example which shows how to compute the eigenvalues and eigenvectors of Hermitian matrix using scipy library −
import numpy as np from scipy.linalg import eigh # Define a Hermitian matrix A = np.array([[2, 1j], [-1j, 3]]) # Compute eigenvalues and eigenvectors eigenvalues, eigenvectors = eigh(A) print("Hermitian Matrix A:") print(A) print("\nEigenvalues:") print(eigenvalues) print("\nEigenvectors:") print(eigenvectors)
Here is the output of computing the eigenvalues and eigenvectors of Hermitian matrix using scipy library −
Hermitian Matrix A: [[ 2.+0.j 0.+1.j] [-0.-1.j 3.+0.j]] Eigenvalues: [1.38196601 3.61803399] Eigenvectors: [[-0.85065081+0.j 0.52573111+0.j ] [ 0. -0.52573111j 0. -0.85065081j]]
Following is the example of computing the eigenvalues and eigenvectors of symmetric matrix using scipy library −
import numpy as np from scipy.linalg import eigh # Define a Symmetric matrix A = np.array([[4, 2], [2, 3]]) # Compute eigenvalues and eigenvectors eigenvalues, eigenvectors = eigh(A) print("Symmetric Matrix A:") print(A) print("\nEigenvalues:") print(eigenvalues) print("\nEigenvectors:") print(eigenvectors)
Here is the output of computing the eigenvalues and eigenvectors of Symmetric matrix using scipy library −
Symmetric Matrix A: [[4 2] [2 3]] Eigenvalues: [1.43844719 5.56155281] Eigenvectors: [[ 0.61541221 -0.78820544] [-0.78820544 -0.61541221]]