Advanced Linear Algebra for ML
A map of the decompositions that run machine learning: which factorisation to reach for, what each one costs, and which trap each one avoids.
Why Does This Exist?
Basic linear algebra teaches you what a matrix is. Advanced linear algebra teaches you what to do with one, and the answer is almost always a decomposition: rewriting the matrix as a product of simpler pieces that expose the property you need. Solving, orthogonalising, ranking, finding axes: each job has a factorisation, each factorisation has a cost, and picking wrong costs real money at scale.
The map, in one list:
- LU: with triangular factors. The workhorse for square systems; factor once, solve many right-hand sides cheaply.
- QR: with orthonormal . Stable least squares and every orthogonalisation routine.
- Cholesky: for symmetric positive definite matrices. Half the cost of LU; the fast path for covariances and kernels.
- Eigendecomposition: (or when symmetric). Axes and long-run behaviour.
- SVD: for any shape. Ranking, compression, the pseudo-inverse that never fails.
Details live on their own pages: linear systems and least squares, singular value decomposition, eigenvalues and eigenvectors. This page is the map that says which one your problem needs.
The one idea you need first
A factorisation rewrites as a product whose pieces are easier than : triangular systems solve by substitution, orthogonal matrices invert by transposing, diagonal matrices power entry by entry. You pay once to factor, then every later operation is cheap. That pay-once structure is the economic argument behind all five entries.
Think of It Like This
A workshop wall of specialised saws
A workshop stocks five saws that all cut wood. The rip saw cuts along the grain fast, the crosscut goes across cleanly, the coping saw turns corners, the hacksaw handles metal, the chainsaw fells trees. Each is the best at one cut and a poor substitute for the others.
Decompositions are that wall. LU rips through square systems, QR crosscuts into stable orthogonal pieces, Cholesky is the lighter saw for symmetric stock, eigendecomposition follows the grain of the matrix's own axes, and SVD cuts anything including shapes the other saws cannot clamp. Reaching for the nearest saw instead of the right one is how bills and numerically mangled answers happen.
How It Actually Works
Match the job to the tool:
- Solve , square and ordinary → LU with partial pivoting ( flops). Factor once; each new costs .
- Least squares or orthonormal basis → QR. Never form the normal equations when QR on is stable and direct.
- Symmetric positive definite system (covariance, kernel, Hessian near a minimum) → Cholesky ( flops, no pivoting needed). If it fails, the matrix was not positive definite and you just learned something.
- Powers, exponentials, long-run behaviour → eigendecomposition, via diagonalisation when the matrix allows it.
- Rank, compression, any shape, failing gracefully → SVD. The most expensive and the most informative.
Worked example
Solve a small system the right way, with substitution instead of an inverse:
Eliminate: subtract of row one from row two, giving against . Back-substitute: , then so . Answer . Check: and . No inverse was formed, no determinant computed; elimination plus substitution did everything.
Code
The point is the check: solve directly and confirm the residual vanishes, instead of forming an inverse.
import numpy as np
A = np.array([[3.0, 1.0], [1.0, 2.0]])b = np.array([9.0, 8.0])x = np.linalg.solve(A, b) # LU under the hood, never an explicit inverseprint(x) # -> [2. 3.]print(np.allclose(A @ x, b)) # -> Truesolve factors and substitutes; inv(A) @ b inverts and multiplies, costing more while amplifying rounding error. Same answer here, different bills and different stability at scale.
Watch Out For
Calling inv when you needed solve
np.linalg.inv(A) @ b is the most common linear-algebra performance bug in ML code: three times the flops of solve, worse error bounds, and an explicit inverse nothing downstream needs. Unless the inverse matrix itself is the deliverable, factor and substitute. Linters will not catch this; only habit does.
Forming the Gram matrix before decomposing
Computing squares the condition number: a mildly ill-conditioned becomes a hopelessly ill-conditioned , and information is destroyed before the solver starts. Factor directly (QR for least squares, SVD for the full picture) and leave the Gram matrix unforged.
The Quick Version
- Advanced linear algebra is choosing decompositions: LU solves, QR orthogonalises, Cholesky speeds symmetric cases, eigendecomposition finds axes, SVD ranks anything.
- Factor once at , then solve each new right-hand side at .
- The 2-by-2 system above solves to by elimination, no inverse involved.
- Never call
invto solve a system; never form when QR or SVD on works. - Cholesky failing is a diagnosis: the matrix was not positive definite.