Hello! Welcome to the third lesson in our module on Mathematics for Machine Learning.
In our last lesson, we explored how matrix multiplication transforms vectors in space. We saw that multiplying a vector by a matrix usually changes both its length and its direction. This led us to a natural question: are there any vectors whose direction remains unchanged by a transformation, even if their length is scaled?
Today, we will answer that question. This lesson introduces eigenvectors and eigenvalues, which are precisely these special vectors and their corresponding scaling factors. Understanding them is fundamental to many advanced AI and data analysis techniques, as they reveal the deepest properties of a linear transformation.
Our goal for this lesson is to calculate eigenvalues and eigenvectors and explain their geometric significance. We will cover:
- The intuitive, geometric meaning of eigenvectors and eigenvalues.
- The step-by-step mathematical process for calculating them.
- How to compute them efficiently using Python's NumPy library.
1. The Geometric Meaning: Special Vectors of a Transformation
Imagine a linear transformation squishing, stretching, and rotating space. Most vectors will be knocked off their original line. However, some special vectors will simply be stretched or shrunk, perfectly maintaining their original direction. These are the eigenvectors. The factor by which they are stretched or shrunk is their corresponding eigenvalue.

To build a strong intuition for this, there's no better resource than 3Blue1Brown.
Eigenvectors and eigenvalues | Chapter 14, Essence of linear algebra
This video from the 'Essence of linear algebra' series provides an excellent visual and conceptual introduction to eigenvectors and eigenvalues.
Watch from the beginning to 05:15. Focus on the core idea: eigenvectors are the vectors that remain on their own span after a transformation, and eigenvalues are the factors by which they are stretched or squished.
As the video explains, this concept is incredibly powerful. For a 3D rotation, the eigenvector is the axis of rotation—the one direction that remains stable. In data analysis, eigenvectors can represent the principal directions of variance in your data (a topic we'll see in Principal Component Analysis, or PCA).
2. How to Calculate Eigenvalues and Eigenvectors
Now that we have the intuition, let's dive into the calculation. The formal definition of an eigenvector and its corresponding eigenvalue for a matrix is:
This equation states that when the matrix transforms the vector , the result is the same as simply scaling by the scalar .
To solve for and , we can't just divide by . We need a bit of algebra. The process involves two main steps: first finding the eigenvalues, then finding the corresponding eigenvectors.
The video below gives a very clear, step-by-step guide to this process.
Finding Eigenvalues and Eigenvectors
Professor Dave Explains provides a methodical walkthrough of the calculation process. We will watch it in two parts.
First, watch from 02:51 to 06:31. This section explains how to find the eigenvalues by setting up and solving the 'characteristic equation'. Next, watch from 06:31 to 11:27. This part shows how to use each eigenvalue you've found to solve for its corresponding eigenvector(s).
Let's summarize the procedure you just saw.
Step 1: Find the Eigenvalues ()
- Start with the defining equation: .
- Rearrange it to find a vector that gets sent to the zero vector.
- This equation states that the matrix transforms a non-zero vector into the zero vector. As you saw in the 3Blue1Brown video, this can only happen if the transformation squishes space into a lower dimension—meaning the matrix is not invertible and its determinant is zero.
- This gives us the characteristic equation, which we can solve for :
The solutions for are the eigenvalues of matrix . For an matrix, this will be a polynomial of degree , giving up to distinct eigenvalues.
Step 2: Find the Eigenvectors ()
- For each eigenvalue you found, plug it back into the equation:
- This gives you a system of linear equations. Solve for the vector .
- The solution will not be a single unique vector, but rather a set of vectors that all lie on the same line (or in the same subspace). As Professor Dave showed, you can "choose" a simple value for one of the components (like ) to get a representative eigenvector. Any scalar multiple of that vector is also a valid eigenvector.
3. From Manual Calculation to Python Code
While it's crucial to understand the manual calculation, in practice, you'll always use a library like NumPy to find eigenvalues and eigenvectors.
The numpy.linalg.eig() function does all this work for you. Let's see how to use it.
22.2. Eigendecompositions - Dive into Deep Learning
The 'Dive into Deep Learning' book provides a concise summary of the math and shows how to use NumPy to perform the calculation.
Read the sections 'Eigendecompositions', 'Finding Eigenvalues', and the Python code examples. This will connect the manual method we just learned to the practical numpy.linalg.eig function.
Let's use the first example from the Professor Dave video and verify it with NumPy.
The matrix was:
He calculated the eigenvalues to be and , with corresponding eigenvectors and .
Now, let's do it in Python:
import numpy as np
# Define the matrix A
A = np.array([[1, 1],
[4, 1]])
# Calculate eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)
print("Eigenvalues from NumPy:")
print(eigenvalues)
print("\nEigenvectors from NumPy:")
print(eigenvectors)
You'll notice two things about the NumPy output:
- The eigenvalues
[ 3., -1.]match the manual calculation perfectly. - The eigenvectors look different:
[[ 0.447, -0.447], [ 0.894, 0.894]].
Why the difference? numpy.linalg.eig returns normalized eigenvectors, meaning their length (L2 norm) is 1. Let's check: the eigenvector for is approximately [0.447, 0.894]. If you divide our manually calculated vector [1, 2] by its length, , you get [1/2.236, 2/2.236], which is [0.447, 0.894]. They are the same vector, just scaled differently. Remember, any non-zero scalar multiple of an eigenvector is still an eigenvector.
4. Why This Matters for AI: A Glimpse into Deep Learning
Eigenvalues are not just a mathematical curiosity. They can tell you about the stability of a system. In a neural network, each layer is essentially a matrix multiplication followed by a non-linear activation. If we simplify and imagine a deep network as just repeated matrix multiplications (), the behavior is dominated by the largest eigenvalue of .
- If the largest eigenvalue's magnitude is > 1, the output vector will grow exponentially with each layer, leading to exploding gradients.
- If the largest eigenvalue's magnitude is < 1, the output vector will shrink to zero, leading to vanishing gradients.
Proper initialization of network weights is designed to keep the largest eigenvalue around 1, ensuring signals can propagate through the network without exploding or vanishing.
22.2. Eigendecompositions - Dive into Deep Learning
To see this effect in action, this section of 'Dive into Deep Learning' simulates a simple iterated map and connects its behavior directly to the matrix's largest eigenvalue.
Read the subsections 'Eigenvectors as Long Term Behavior', 'Behavior on Random Data', and 'Relating Back to Eigenvectors'. The key insight is that after many applications of a matrix, a random vector will align with the eigenvector corresponding to the largest eigenvalue.
Conclusion
In this lesson, we demystified the concepts of eigenvalues and eigenvectors. They are the intrinsic directions and scaling factors of a linear transformation, revealing its fundamental properties.
Key Takeaways:
- Geometric Significance: Eigenvectors are special vectors that a matrix transformation only scales, without changing their direction. The eigenvalue is that scaling factor.
- Calculation: Eigenvalues () are found by solving the characteristic equation . Eigenvectors () are then found by solving for each eigenvalue.
- Practical Computation: In practice, you will use functions like
numpy.linalg.eig()to compute them instantly. - Relevance to AI: Eigenvalues are critical for analyzing the stability of dynamic systems, including deep neural networks, where they help us understand and prevent issues like exploding or vanishing gradients.
In our next lesson, we will shift our focus from transformations to optimization. We will learn how to calculate derivatives and partial derivatives, which measure rates of change. This will lead us directly to the concept of the gradient, the single most important tool for training machine learning models.