
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Eigenvectors
What are Eigenvectors?
Eigenvectors are special vectors associated with a matrix that provide information about the matrix's properties.
In the context of linear algebra, if A is a square matrix, an eigenvector v corresponding to an eigenvalue is a non-zero vector that satisfies the equation −
Av = v
This means that when the matrix A multiplies the vector v, the result is the same as multiplying the vector v by the scalar .
Computing Eigenvectors in NumPy
NumPy provides the numpy.linalg.eig() function to compute the eigenvalues and eigenvectors of a square matrix. Let us see how this function works with an example.
Example
In this example, the eigenvalues of the matrix A are 3 and 2. The corresponding eigenvectors are shown in the output −
import numpy as np # Define a 2x2 matrix A = np.array([[4, -2], [1, 1]]) # Compute the eigenvalues and eigenvectors eigenvalues, eigenvectors = np.linalg.eig(A) print("Eigenvalues:", eigenvalues) print("Eigenvectors:\n", eigenvectors)
The output from numpy.linalg.eig() function contains two arrays: one for eigenvalues and one for eigenvectors.
The eigenvalues array contains the eigenvalues of the matrix, and each column of the eigenvectors array represents an eigenvector corresponding to the respective eigenvalue −
Eigenvalues: [3. 2.] Eigenvectors: [[ 0.89442719 0.70710678] [ 0.4472136 -0.70710678]]
Properties of Eigenvectors
Eigenvectors have several important properties, they are −
- Linearity: Eigenvectors corresponding to different eigenvalues are linearly independent.
- Scalability: Any scalar multiple of an eigenvector is also an eigenvector corresponding to the same eigenvalue.
- Invariance: Eigenvectors remain unchanged (up to a scalar multiple) under the linear transformation defined by the matrix.
- Orthogonality: In the case of symmetric matrices, eigenvectors corresponding to distinct eigenvalues are orthogonal.
Applications of Eigenvectors
Eigenvectors have numerous applications, they are −
- Principal Component Analysis (PCA): Used in data analysis and machine learning for dimensionality reduction.
- Stability Analysis: Used in control theory to analyze the stability of systems.
- Quantum Mechanics: Used to solve the Schrdinger equation and find the energy levels of a system.
- Vibration Analysis: Used in engineering to analyze the natural frequencies of structures.
- Graph Theory: Used to analyze the properties of graphs and networks.
Example: Eigenvectors of a 3x3 Matrix
In the following example, we are computig the eigenvalues and eigenvectors of a 3x3 matrix using NumPy −
import numpy as np # Define a 3x3 matrix B = np.array([[1, 2, 3], [0, 1, 4], [5, 6, 0]]) # Compute the eigenvalues and eigenvectors eigenvalues, eigenvectors = np.linalg.eig(B) print("Eigenvalues:", eigenvalues) print("Eigenvectors:\n", eigenvectors)
This will produce the following result −
Eigenvalues: [-5.2296696 -0.02635282 7.25602242] Eigenvectors: [[ 0.22578016 -0.75769839 -0.49927017] [ 0.52634845 0.63212771 -0.46674201] [-0.81974424 -0.16219652 -0.72998712]]
Symmetric Matrices and Real Eigenvectors
A symmetric matrix is a matrix that is equal to its transpose (i.e., A = AT). Symmetric matrices have some special properties regarding their eigenvalues and eigenvectors −
- Real Eigenvalues: The eigenvalues of a symmetric matrix are always real numbers.
- Orthogonal Eigenvectors: The eigenvectors of a symmetric matrix corresponding to distinct eigenvalues are orthogonal.
Example
Let us compute the eigenvalues and eigenvectors of a symmetric matrix −
import numpy as np # Define a symmetric matrix C = np.array([[4, 1, 1], [1, 4, 1], [1, 1, 4]]) # Compute the eigenvalues and eigenvectors eigenvalues, eigenvectors = np.linalg.eig(C) print("Eigenvalues:", eigenvalues) print("Eigenvectors:\n", eigenvectors)
Following is the output of the above code −
Eigenvalues: [6. 3. 3.] Eigenvectors: [[-0.57735027 -0.81649658 -0.15430335] [-0.57735027 0.40824829 -0.6172134 ] [-0.57735027 0.40824829 0.77151675]]
Eigenvectors and Diagonalization
A square matrix A is said to be diagonalizable if it can be written as −
A = PDP-1
where, D is a diagonal matrix containing the eigenvalues of A, and P is a matrix whose columns are the eigenvectors of A.
Example
Let us see how to diagonalize a matrix using NumPy −
import numpy as np # Define a matrix D = np.array([[2, 0, 0], [1, 3, 0], [4, 5, 6]]) # Compute the eigenvalues and eigenvectors eigenvalues, eigenvectors = np.linalg.eig(D) # Diagonal matrix of eigenvalues D_diag = np.diag(eigenvalues) # Reconstruct the original matrix reconstructed_D = eigenvectors @ D_diag @ np.linalg.inv(eigenvectors) print("Original matrix:\n", D) print("Reconstructed matrix:\n", reconstructed_D)
The original matrix is successfully reconstructed using its eigenvalues and eigenvectors, demonstrating the process of diagonalization −
Original matrix: [[2 0 0] [1 3 0] [4 5 6]] Reconstructed matrix: [[2. 0. 0.] [1. 3. 0.] [4. 5. 6.]]