j-devlog
KO
~/nav

// categories

// tags

[MMD]

Linear Algebra Basics Chapter 4: Eigenvalues, Eigenvectors, and PCA

·15 min read·

In Chapter 3 we covered vector operations, linear transformations, matrix multiplication, and inverses. In this final installment, we'll interpret determinants geometrically, dive deep into eigenvalues and eigenvectors — the crown jewel of linear algebra — and trace exactly how they power PCA (Principal Component Analysis) in machine learning.

This post is based on Week 4 of DeepLearning.AI's Mathematics for Machine Learning and Data Science course.

What You'll Learn in This Chapter#

  • Interpreting determinants as area (or volume)
  • The product rule and inverse rule for determinants
  • The concepts of Basis and Span
  • Intuition and computation of eigenvalues and eigenvectors
  • PCA — dimensionality reduction using variance, covariance, and eigenvectors
  • Markov matrices and discrete dynamical systems

Singularity and Rank of Linear Transformations#

Linear transformations, like matrices, can be classified as singular or non-singular.

Transformation TypeResultRank
Non-singular transformationCovers the entire plane2 (in 2D)
Singular transformation (line)Maps onto a single line1
Singular transformation (point)Collapses to the origin0

A non-singular linear transformation covers every point in the plane. A singular transformation compresses the plane onto a line or a point.


Determinant = Area (Geometric Interpretation)#

A determinant is more than just a number. It represents the scaling factor of area (or volume) — how much a linear transformation stretches or shrinks space.


A=[abcd]A = \begin{bmatrix} a & b \\ c & d \end{bmatrix}


Apply this transformation to the unit square (area = 1), and the resulting parallelogram has area det(A)|\det(A)|.

Determinant ValueMeaning
det=0\det = 0Space collapses to a line or point (area = 0)
det>0\det > 0Space expands, orientation preserved
det<0\det < 0Space expands, orientation reversed (reflection)

Key insight: A singular matrix compresses area down to 0, which is exactly why det=0\det = 0.


Determinant of a Product#

det(AB)=det(A)det(B)\det(AB) = \det(A) \cdot \det(B)


The area scaling factor of two composed transformations equals the product of each transformation's scaling factor.

Important consequence:


A or B is singulardet(AB)=0AB is also singularA \text{ or } B \text{ is singular} \Rightarrow \det(AB) = 0 \Rightarrow AB \text{ is also singular}


The product of a singular and a non-singular matrix is always singular.


Determinant of an Inverse#

When AA is non-singular (invertible):


det(A1)=1det(A)\det(A^{-1}) = \frac{1}{\det(A)}


Intuition: If AA stretches space by a factor of 5, then A1A^{-1} must shrink it by 15\frac{1}{5} to undo the transformation.

Examples:


det(M)=0.2det(M1)=5\det(M) = 0.2 \Rightarrow \det(M^{-1}) = 5


det(N)=0.125det(N1)=8\det(N) = 0.125 \Rightarrow \det(N^{-1}) = 8


Basis#

A basis is the minimal set of vectors that defines a vector space.


e1=[10],e2=[01]\vec{e_1} = \begin{bmatrix} 1 \\ 0 \end{bmatrix}, \quad \vec{e_2} = \begin{bmatrix} 0 \\ 1 \end{bmatrix}


The defining property of a basis: every point in the space can be expressed as a linear combination of the basis vectors.


[35]=3e1+5e2\begin{bmatrix} 3 \\ 5 \end{bmatrix} = 3\vec{e_1} + 5\vec{e_2}


Requirements to form a basis:

  • The vectors must be linearly independent (no vector can be expressed as a linear combination of the others)
  • Vectors pointing in the same or opposite directions cannot form a basis

Span#

The span of a set of vectors is the set of all points reachable by taking every possible linear combination of those vectors.

Number of VectorsSpan
1 vectorA line (1-dimensional)
2 linearly independent vectorsA plane (2-dimensional)
2 linearly dependent vectorsA line (1-dimensional only)

Basis = minimal spanning set: the smallest number of linearly independent vectors needed to span the space.

If you have more vectors than the dimension of the space, there will always be linear dependence among them.


Eigenbasis#

When a linear transformation AA is applied, most vectors change direction. But certain special vectors preserve their direction and only change in magnitude (scale). These are called eigenvectors.


Av=λvA\vec{v} = \lambda\vec{v}


  • v\vec{v}: eigenvector (direction preserved)
  • λ\lambda: eigenvalue (scaling factor)

Intuition: When you look at data along an eigenvector direction, the matrix multiplication (a complex operation) reduces to a simple scalar multiplication.


Computing Eigenvalues and Eigenvectors#

The Core Formula#

Av=λvA\vec{v} = \lambda\vec{v} AvλIv=0A\vec{v} - \lambda I\vec{v} = \vec{0} (AλI)v=0(A - \lambda I)\vec{v} = \vec{0}


For v0\vec{v} \neq \vec{0} to exist, (AλI)(A - \lambda I) must be a singular matrix:


det(AλI)=0(characteristic polynomial)\det(A - \lambda I) = 0 \quad \text{(characteristic polynomial)}

2×2 Matrix Example#

A=[5133]A = \begin{bmatrix} 5 & 1 \\ 3 & 3 \end{bmatrix}


Step 1 — Characteristic polynomial:


det(AλI)=det[5λ133λ]=0\det(A - \lambda I) = \det\begin{bmatrix} 5-\lambda & 1 \\ 3 & 3-\lambda \end{bmatrix} = 0


(5λ)(3λ)3=0(5-\lambda)(3-\lambda) - 3 = 0


λ28λ+12=0\lambda^2 - 8\lambda + 12 = 0


(λ6)(λ2)=0(\lambda - 6)(\lambda - 2) = 0


Eigenvalues: λ1=6,λ2=2\lambda_1 = 6,\quad \lambda_2 = 2

Step 2 — Eigenvector for λ1=6\lambda_1 = 6:


(A6I)v=0(A - 6I)\vec{v} = \vec{0}


[1133]v=0\begin{bmatrix} -1 & 1 \\ 3 & -3 \end{bmatrix}\vec{v} = \vec{0}


v1+v2=0v1=v2v1=[11]-v_1 + v_2 = 0 \Rightarrow v_1 = v_2 \Rightarrow \vec{v_1} = \begin{bmatrix} 1 \\ 1 \end{bmatrix}


Eigenvector for λ2=2\lambda_2 = 2:


[3131]v=0\begin{bmatrix} 3 & 1 \\ 3 & 1 \end{bmatrix}\vec{v} = \vec{0}


3v1+v2=0v2=[13]3v_1 + v_2 = 0 \Rightarrow \vec{v_2} = \begin{bmatrix} 1 \\ -3 \end{bmatrix}

Eigenvalues and eigenvectors always come in pairs.

Number of Eigenvectors#

Matrix SizeEigenvalue CharacteristicsNumber of Eigenvectors
2×22 distinct eigenvaluesAlways 2
2×2Repeated eigenvalue1 or 2
3×33 distinct eigenvaluesAlways 3
3×3One repeated eigenvalue2 or 3
3×3One eigenvalue with multiplicity 31 to 3

Note: Eigenvalues and eigenvectors can only be computed for square matrices.


Dimensionality Reduction and Projection#

The goal of dimensionality reduction is to map high-dimensional data into a lower-dimensional space while preserving as much information as possible.

Projection Formula#

Projecting a data point x\vec{x} onto a unit vector u^\hat{u}:


projection=(xu^)\text{projection} = (\vec{x} \cdot \hat{u})


  • xu^\vec{x} \cdot \hat{u}: the dot product gives the projection value
  • Normalizing u^\hat{u} to a unit vector (u^=1\|\hat{u}\| = 1) ensures no stretching

The amount of information preserved depends on the direction you project onto. To minimize information loss, you should choose the direction of maximum variance (spread).


Variance and Covariance#

Understanding PCA requires two statistical concepts.

Variance#

How spread out the data is from its mean.


Var(X)=1ni=1n(xixˉ)2\text{Var}(X) = \frac{1}{n}\sum_{i=1}^n (x_i - \bar{x})^2

Covariance#

Whether two variables tend to move together in the same direction.


Cov(X,Y)=1ni=1n(xixˉ)(yiyˉ)\text{Cov}(X, Y) = \frac{1}{n}\sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y})

Covariance ValueMeaning
PositiveWhen XX is large, YY tends to be large too (same direction)
0No linear relationship between the two variables
NegativeWhen XX is large, YY tends to be small (opposite direction)

Even if the mean and variance are identical, different covariance means the data patterns are completely different.


Covariance Matrix#

A matrix that captures all variances and covariances among multiple variables.


Σ=[Var(X)Cov(X,Y)Cov(Y,X)Var(Y)]\Sigma = \begin{bmatrix} \text{Var}(X) & \text{Cov}(X,Y) \\ \text{Cov}(Y,X) & \text{Var}(Y) \end{bmatrix}


  • Diagonal entries: variance of each variable
  • Off-diagonal entries: covariance between pairs of variables
  • Symmetric matrix: Cov(X,Y)=Cov(Y,X)\text{Cov}(X,Y) = \text{Cov}(Y,X)

Matrix notation:


Σ=1n(Xμ)T(Xμ)\Sigma = \frac{1}{n}(X - \mu)^T(X - \mu)


Here XμX - \mu is the mean-centered (centered) data matrix.


PCA — Principal Component Analysis#

PCA combines projection + eigenvalues/eigenvectors + covariance matrix to reduce dimensionality while preserving as much information as possible.

Core Idea#

The eigenvectors of the covariance matrix = the directions along which the data spreads the most.

The eigenvector corresponding to the largest eigenvalue = the direction containing the most information (the 1st principal component).

PCA Step by Step#

Step 1 — Center the data

Subtract the mean of each column so every feature has zero mean.


Xcentered=XμX_{\text{centered}} = X - \mu


Step 2 — Compute the covariance matrix


Σ=1nXcenteredTXcentered\Sigma = \frac{1}{n}X_{\text{centered}}^T X_{\text{centered}}


Step 3 — Compute eigenvalues and eigenvectors


Σv=λv\Sigma \vec{v} = \lambda \vec{v}


Sort eigenvalues in descending order: (λ1λ2\lambda_1 \geq \lambda_2 \geq \cdots)

Step 4 — Build the projection matrix

Select as many eigenvectors as the target number of dimensions to form the projection matrix VV. (Normalize each eigenvector to a unit vector.)

Step 5 — Project the data


XPCA=XcenteredVX_{\text{PCA}} = X_{\text{centered}} \cdot V

Why Does It Work?#

The covariance matrix captures the direction and magnitude of the data's spread. Viewed as a linear transformation, the eigenvectors are the directions where the transformation only scales without rotating, and the eigenvalues tell us how much it scales.

Projecting onto the direction of the largest eigenvector → maximum variance (information) is preserved.

In practice: scikit-learn's PCA(n_components=2) performs this entire process automatically. It's widely used for facial recognition, noise reduction, and data visualization.

from sklearn.decomposition import PCA
import numpy as np

X = np.array([[2.5, 2.4], [0.5, 0.7], [2.2, 2.9],
              [1.9, 2.2], [3.1, 3.0], [2.3, 2.7]])

pca = PCA(n_components=1)
X_pca = pca.fit_transform(X)

print("Eigenvalues:", pca.explained_variance_)
print("Eigenvectors:", pca.components_)
print("Transformed data:", X_pca)

Markov Matrices and Discrete Dynamical Systems#

One practical application of eigenvectors is Markov matrices.

Markov Matrix#

  • All entries are non-negative
  • Each column sums to 1 — represents state transition probabilities

M=[0.90.20.10.8]M = \begin{bmatrix} 0.9 & 0.2 \\ 0.1 & 0.8 \end{bmatrix}


(Example: sunny day → stays sunny with 90% probability, sunny day → becomes cloudy with 10% probability)

Updating the State Vector#

From the current state st\vec{s}_t, the next state is:


st+1=Mst\vec{s}_{t+1} = M \cdot \vec{s}_t


Repeated application causes the state vector to converge (reach equilibrium).


s=equilibrium vector\vec{s}_\infty = \text{equilibrium vector}


Equilibrium vector = eigenvector corresponding to eigenvalue 1

Markov matrices always have eigenvalue 1, so the long-run steady state can be found as an eigenvector.

In practice: Google's PageRank algorithm uses a Markov matrix to rank web pages.


Full Summary#

ConceptDescription
Determinant = AreaHow much a linear transformation scales the unit square
det(AB)=det(A)det(B)\det(AB) = \det(A)\det(B)Determinant of a product = product of determinants
det(A1)=1/det(A)\det(A^{-1}) = 1/\det(A)Determinant of an inverse = reciprocal
BasisMinimal set of linearly independent vectors that span the space
SpanSet of all points reachable by linear combinations of vectors
EigenvectorA vector whose direction is unchanged by the transformation
EigenvalueThe scaling factor applied to an eigenvector
Characteristic polynomialSolve det(AλI)=0\det(A - \lambda I) = 0 to find eigenvalues
Covariance matrixSymmetric matrix encoding variance and covariance between variables
PCADimensionality reduction that maximally preserves information using eigenvectors of the covariance matrix
Markov matrixState transition probability matrix; equilibrium = eigenvector


Quiz#

Q1. Given det(A)=4\det(A) = 4 and det(B)=3\det(B) = -3, find det(AB)\det(AB) and det(A1)\det(A^{-1}).

Show Answer

det(AB)=det(A)det(B)=4×(3)=12\det(AB) = \det(A) \cdot \det(B) = 4 \times (-3) = -12


det(A1)=1det(A)=14=0.25\det(A^{-1}) = \frac{1}{\det(A)} = \frac{1}{4} = 0.25


Since det(AB)<0\det(AB) < 0, this transformation reverses orientation.


Q2. Can the set {(1,2), (4,8)}\{(1, 2),\ (4, 8)\} form a basis for R2\mathbb{R}^2?

Show Answer

Since (4,8)=4(1,2)(4, 8) = 4 \cdot (1, 2), the two vectors are linearly dependent.

Vectors pointing in the same direction only span a line, so they cannot form a basis for R2\mathbb{R}^2.


Q3. Find the eigenvalues of the following matrix.


A=[3102]A = \begin{bmatrix} 3 & 1 \\ 0 & 2 \end{bmatrix}


Show Answer

det(AλI)=det[3λ102λ]=(3λ)(2λ)=0\det(A - \lambda I) = \det\begin{bmatrix} 3-\lambda & 1 \\ 0 & 2-\lambda \end{bmatrix} = (3-\lambda)(2-\lambda) = 0


Eigenvalues: λ1=3,λ2=2\lambda_1 = 3,\quad \lambda_2 = 2

(For a triangular matrix, the eigenvalues are simply the diagonal entries.)


Q4. Why do we choose the eigenvector corresponding to the largest eigenvalue in PCA?

Show Answer

The eigenvalue indicates how much the data spreads (variance) in that direction.

The largest eigenvalue = the direction of maximum variance = the direction that preserves the most information.

Therefore, to minimize information loss when reducing dimensions, we should choose the eigenvector direction with the largest eigenvalue.


Q5. The equilibrium vector of a Markov matrix MM is the eigenvector of what?

Show Answer

It is the eigenvector corresponding to eigenvalue 1 of the Markov matrix.


Ms=1s=sM\vec{s} = 1 \cdot \vec{s} = \vec{s}


In other words, the equilibrium vector is a fixed point — a state vector that no longer changes no matter how many times you apply the update rule — and this is precisely the eigenvector for eigenvalue 1.


This concludes the Linear Algebra Basics series. Here's a recap of the journey from Chapter 1 to Chapter 4:

ChapterKey Topics
Chapter 1Systems of equations, singularity, determinants
Chapter 2Elimination, row echelon form, rank, Gaussian elimination
Chapter 3Vectors, linear transformations, matrix multiplication, inverses, perceptrons
Chapter 4Eigenvalues/eigenvectors, PCA, Markov matrices

// Related Posts