Matrix decomposition (factorization) expresses a complex matrix as a product of canonical, interpretable constituent components. In modern computing, spectral decompositions and Singular Value Decomposition (SVD) power:
Principal Component Analysis (PCA) for dimensionality reduction.
Google's original PageRank algorithm and quantum mechanics state vectors.
NumPy's np.linalg module provides production-grade LAPACK bindings for these algorithms.
1. Eigenvalues and Eigenvectors: np.linalg.eig()
For a square matrix $A$, an eigenvector $\mathbf{v}$ and corresponding eigenvalue $\lambda$ satisfy:
$$A \mathbf{v} = \lambda \mathbf{v}$$
This states that multiplying matrix $A$ by vector $\mathbf{v}$ does not alter its direction—it merely scales it by factor $\lambda$.
1import numpy as np
2
3A = np.array([
4[2.0,1.0],
5[1.0,2.0]
6])
7
8# Compute eigenvalues and eigenvectors
9eigenvalues, eigenvectors = np.linalg.eig(A)
10
11print("Eigenvalues: ", eigenvalues)
12print("Eigenvectors:
13", eigenvectors)
14
15# Verification: A @ v == lambda * v for the first eigenpair
16v0 = eigenvectors[:,0]
17lambda0 = eigenvalues[0]
18
19print("
20A @ v0: ", A @ v0)
21print("lambda0 * v0:", lambda0 * v0)
Output:
1Eigenvalues: [3. 1.]
2Eigenvectors:
3 [[ 0.70710678 -0.70710678]
4 [ 0.70710678 0.70710678]]
5
6A @ v0: [2.12132034 2.12132034]
7lambda0 * v0: [2.12132034 2.12132034]
Crucial Array Structure: The eigenvectors in NumPy are returned as column vectors! The $i$-th eigenvector is accessed as eigenvectors[:, i], NOT eigenvectors[i].
A grayscale image of size $1000 \times 1000$ requires $1,000,000$ numbers. By truncating the SVD to the top $k$ singular values (low-rank approximation), we can reconstruct high-fidelity images using a tiny fraction of the data: