CARVIEW |
How to Calculate Eigenvalues and Eigenvectors with NumPy
Eigenvalues and eigenvectors are fundamental concepts in linear algebra that may be applied to many applications, such as in revealing the stability of a system, or for dimensionality reduction.

Image by Editor | Ideogram
NumPy is a powerful Python library, which supports many mathematical functions that can be applied to multi-dimensional arrays.
In this short tutorial, you will learn how to calculate the eigenvalues and eigenvectors of an array using the linear algebra module in NumPy.
Calculating the Eigenvalues and Eigenvectors in NumPy
In order to explore the function through which we can calculate the eigenvalues and eigenvectors of an array in NumPy, we will be directing our attention to the linalg
module.
linalg
is a NumPy module that aggregates a collection of functions for performing linear algebra (hence the name of the module) operations. The function that we will be exploring is found within this module and is called np.linalg.eig
.
The np.linalg.eig
function takes a square array as input and outputs a tuple containing the eigenvalues and eigenvectors of the input array. Both input and outputs of this function may be real or complex, as we will see later in this tutorial.
But let’s first dedicate some time to touch upon the mathematical detail related to the calculation of eigenvalues and eigenvectors. This will help us in interpreting the different outputs that may be generated by the np.linalg.eig
function, when we try it out in practice later.
Touching Briefly Upon the Mathematical Detail
The information contained within eigenvalues and eigenvectors can help us to understand important properties of the data that we might be working with.
For example, in Principal Component Analysis (PCA), the calculated eigenvectors can help us to identify the directions in which the data varies the most, while the corresponding eigenvalues provide us with the magnitude of such variations. In keeping only those eigenvectors that correspond to the largest eigenvalues, we can effectively reduce the dimensionality of the input data.
Let’s take an array A of dimensions n x n, which contains the data (or observations) that we would like to analyse by finding its eigenvectors and eigenvalues. For this array, A, each eigenvector and corresponding eigenvalue need to satisfy the following equation:
where:
- v of size n x 1 contains an eigenvector of A,
- λ is a scalar eigenvalue that corresponds to the eigenvector in v.
The above equation relies on having A of square dimensions, so that the array dimensions on the left and right hand sides of the equation can match as follows:
If A had to be of size m x n, where m ≠ n, then we would end up with a mismatch of array dimensions between the left and right hand sides of the equation, which cannot be equated directly:
Let’s next try to calculate the eigenvectors and eigenvalues using NumPy.
Trying it Out in Practice
Using the following code, we can calculate the eigenvectors and eigenvalues of a randomly generated 3 x 3 array of integers:
# Import NumPy package
import numpy as np
# Define 2D array of random integer values
A = np.random.randint(0, 10, (3, 3))
# Print the 2D array
print('A: \n', A, '\n')
# Calculate the eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)
# Print the eigenvalues and eigenvectors
print('Eigenvalues: \n', eigenvalues, '\n')
print('Eigenvectors: \n', eigenvectors)
The generated output for the specific values of A
is:
A:
[[8 8 4]
[6 9 6]
[0 5 2]]
Eigenvalues:
[17.11866554 3.19694517 -1.31561071]
Eigenvectors:
[[-0.69651195 0.76761449 0.11596078]
[-0.68125597 -0.14921139 -0.5489251 ]
[-0.22530294 -0.62330085 0.82778882]]
If you had to run the code above repeatedly, you will be able to notice that:
- An input array A of size n x n can have up to n linearly independent eigenvectors. The code above returns these in the array
eigenvectors
, where each column of the array holds an eigenvector. - Each eigenvector has an associated eigenvalue, which means that the vector
eigenvalues
will contain as many values as there are columns ineigenvectors
. These eigenvalues are returned by thenp.linalg.eig
function in no specific order, can be distinct or repeated and real or complex. - If an input array A is characterized by repeated eigenvalues, then this indicates that A may not have a full set of n linearly independent eigenvectors. Whether it does depends on the eigenvalues’ geometric multiplicity.
- A real-valued array A can possess complex-valued solutions. A complex eigenvalue is accompanied by a corresponding complex eigenvector, and you will find that complex eigenvalues and eigenvectors occur in conjugate pairs.
- A complex-valued array A can possess real or complex eigenvalues depending on its particular properties. For example, a Hermitian array (which is a complex square array that is equal to its own conjugate transpose) can generate real eigenvalues and complex eigenvectors. To try this out, go back to the code above and change A to:
A = np.array([[1, 1j], [-1j, 1]])
Stefania Cristina, PhD, is a Senior Lecturer with the Department of Systems and Control Engineering at the University of Malta. Her research interests lie within the domains of computer vision and machine learning.
- Calculate Computational Efficiency of Deep Learning Models with…
- Vector and Matrix Norms with NumPy Linalg Norm
- Beyond Numpy and Pandas: Unlocking the Potential of Lesser-Known…
- Introduction to Numpy and Pandas
- Using NumPy to Perform Date and Time Calculations
- NumPy for Simulating Random Processes and Monte Carlo Methods
Latest Posts
- 5 NotebookLM Tips to Make Your Day a Little Easier
- We Benchmarked DuckDB, SQLite, and Pandas on 1M Rows: Here’s What Happened
- Prompt Engineering Templates That Work: 7 Copy-Paste Recipes for LLMs
- A Complete Guide to Seaborn
- 10 Command-Line Tools Every Data Scientist Should Know
- How I Actually Use Statistics as a Data Scientist
Top Posts |
---|
- We Benchmarked DuckDB, SQLite, and Pandas on 1M Rows: Here’s What Happened
- How I Actually Use Statistics as a Data Scientist
- The Lazy Data Scientist’s Guide to Exploratory Data Analysis
- Prompt Engineering Templates That Work: 7 Copy-Paste Recipes for LLMs
- 10 Command-Line Tools Every Data Scientist Should Know
- A Gentle Introduction to TypeScript for Python Programmers
- A Complete Guide to Seaborn
- 5 Fun AI Agent Projects for Absolute Beginners
- From Excel to Python: 7 Steps Analysts Can Take Today
- A Gentle Introduction to MCP Servers and Clients