10.3 Least Squares, Eigen Decomposition, and SVD: From Exact Solutions to Best Approximations
Real observations rarely satisfy one exact linear system. Thousands of request-load and latency records may need to be approximated with only dozens of features, so the equations greatly outnumber the unknowns. Linear algebra turns “they cannot all be exact” into a measurable best-approximation problem.
Learning Objectives
- Understand least squares from a projection perspective;
- Explain invariant directions represented by eigenvalues and eigenvectors;
- Grasp that singular value decomposition applies to any matrix;
- Clarify the centering, dimensionality reduction, and information loss boundaries in PCA.
1. Least Squares: Minimizing Residuals
When
The residual
leading to the normal equations:
Geometrically,
The fitted projection lstsq returns one with minimum Euclidean norm. Least squares also applies when an exact solution exists.
For full-column-rank lstsq; it does not assume automatic selection of a method best suited to the application.
import numpy as np
# y ≈ slope * x + intercept
x = np.array([0.0, 1.0, 2.0, 3.0])
y = np.array([1.1, 2.9, 5.2, 6.8])
A = np.column_stack([x, np.ones_like(x)])
solution, residuals, rank, singular_values = np.linalg.lstsq(
A, y, rcond=None
)
slope, intercept = solution
print(f"{slope:.2f} {intercept:.2f} {rank}") # 1.94 1.09 2
residual = y - A @ solution
print(f"{residual @ residual:.3f}") # 0.082
print(np.allclose(A.T @ residual, 0.0)) # TrueLeast squares implicitly assumes an error metric: large residuals are penalized more heavily due to squaring, making the method sensitive to outliers. When data violate assumptions such as independence or homoscedasticity, parameter estimates may still be computed, but statistical interpretation requires additional validation.
residuals contains sums of squared residuals, not per-sample errors. It is empty when rank is below the column count or rows do not outnumber columns; empty does not mean zero fitting error. Compute y - A @ solution directly when needed. Explicit rcond=None uses machine precision times the larger matrix dimension as the relative rank threshold. NumPy’s documentation records the default change in 2.0.
2. Eigenvectors: Directions That Remain Unchanged Under Transformation
For a square matrix
then
Eigenvalues can help us understand:
- The growth or decay of a linear dynamical system along different directions;
- The structural properties encoded in adjacency and Laplacian matrices of graphs;
- The principal directions of variation in symmetric covariance matrices.
Not all matrices have a complete set of eigenvectors in the real number domain. Matrices may possess complex eigenvalues, or may lack sufficient linearly independent eigenvectors to be diagonalized. Therefore, "computing eigen decomposition" is not a universally applicable or unconditional step for arbitrary matrices.
A 90° planar rotation has no real eigenvectors, while [[1,1],[0,1]] has only a one-dimensional eigenspace and is not diagonalizable. A real symmetric matrix does admit an orthonormal eigenbasis, for which libraries provide eigh. A repeated eigenvalue can have more than one choice of basis.
3. SVD: Directions and Magnitudes of Any Matrix
Any real matrix
- The columns of
represent orthogonal directions in the input space; - The non-negative diagonal entries of
are the singular values, indicating the scaling magnitude along each direction; - The columns of
represent orthogonal directions in the output space.
The transformation can be viewed as a three-step process: first rotate or reflect the input via
In the full SVD, Vh, which equals V.T for real matrices. Multiplying as though that result were V reverses the intended orientation.
In exact arithmetic, the number of nonzero singular values equals the rank. For an invertible square matrix,
4. Low-Rank Approximation and Compression
Arrange the singular values in descending order and retain only the first
For
This technique can be applied to image compression, latent semantic analysis, and recommendation models. However, "small" does not automatically imply "noise." Rare patterns or outliers may also lie along low-energy directions. Before compression, it is essential to examine the task context to determine whether such low-energy components are meaningful or should be discarded.
5. The Relationship Between PCA and SVD
Principal Component Analysis (PCA) identifies the orthogonal directions that maximize data variance. When the data matrix
The covariance matrix is proportional to
the first few columns of
For
import numpy as np
X = np.array([
[2.0, 1.0],
[3.0, 2.0],
[4.0, 2.5],
[5.0, 4.0],
])
mean = X.mean(axis=0)
centered = X - mean
_, singular_values, vt = np.linalg.svd(centered, full_matrices=False)
principal_direction = vt[0]
one_dimensional = centered @ principal_direction
reconstructed = np.outer(one_dimensional, principal_direction) + mean
ratio = singular_values[0] ** 2 / np.sum(singular_values ** 2)
error = np.linalg.norm(X - reconstructed, "fro")
print(f"{ratio:.4%} {error:.6f}") # 99.0588% 0.301962
print(np.isclose(error, singular_values[1])) # TrueIf the features have vastly different scales, whether to standardize beforehand depends on the specific problem: PCA is sensitive to scale, and standardization alters the meaning of "maximum variance." The mean, scale, and principal components derived from the training data must be fixed and applied consistently to new data.
Here one retained direction leaves a Frobenius reconstruction error equal to the discarded singular value. Changing a singular vector’s overall sign does not change reconstruction; equal singular values also permit rotations within their subspace. Compare reconstructions and subspaces across implementations, rather than requiring identical vector signs. See NumPy’s SVD shape conventions.
6. Which Tool to Choose
| Question | Common Tools | Key Reminders |
|---|---|---|
| Full-rank square linear systems | LU, specialized solve | Avoid explicit inversion |
| Overdetermined systems and fitting | QR, SVD, lstsq | Check residuals, rank, and data assumptions |
| Principal directions of symmetric matrices | Eigenvalue decomposition | Use the symmetric structure |
| Matrix rank and low-rank approximation | SVD | Pay attention to condition number and truncation error |
| Data dimensionality reduction | PCA/SVD after centering | Scaling strategy belongs to model definition |
Common Misconceptions
- PCA automatically identifies the most important business features: It maximizes variance, but has no understanding of business objectives.
- Retaining 95% of variance preserves 95% of information: This is a heuristic tied to task relevance, not a universal equivalence.
- The direction with the largest eigenvalue is always the most stable: Stability also depends on matrix properties, spectral gaps, and perturbation sensitivity.
- SVD is just a theoretical decomposition: It is, in fact, a foundational numerical tool underpinning least squares, pseudoinverses, dimensionality reduction, and condition analysis.
Exercise
- Explain why the least-squares residuals are orthogonal to the column space.
- Compute the eigenvalues and eigenvectors of the diagonal matrix
diag(4, 1), and describe the geometric transformation it represents. - Use SVD to approximate a grayscale matrix as a rank-1 matrix, and compare the reconstruction error.
- Perform PCA on the same dataset first with only centering, and then with standardization followed by centering. Explain why the principal directions differ between the two approaches.
Summary
The next chapter moves from static transformations to continuous change: how functions behave in a small neighborhood around a point, and how many such local changes accumulate to form the overall behavior.