ML Systems Lab Open interactive version →
Foundational 26 min read linear algebramatricesnorms

Vectors & Matrices

Dot product, matrix operations, rank, norms

You have 10,000 training images, each 64×64 pixels × 3 channels = 12,288 numbers per image. These 10,000 images form a matrix X ∈ ℝ^{10000 × 12288}. Every ML operation on this dataset — normalizing features, computing covariances, running regression, doing PCA, forward passes through a neural network — is a matrix operation. If you do not know what a matrix product computes geometrically, you are manipulating ML pipelines you cannot understand or debug.

Vectors carry direction and magnitude. The dot product a·b = ‖a‖ ‖b‖ cos(θ) measures directional alignment — it is large and positive when two vectors point in similar directions, and zero when they are perpendicular. Orthogonal vectors have dot product zero. This is why cosine similarity works for semantic search: aligned embeddings have high dot products, reflecting similar meaning.

Matrix multiplication AB = C means C[i,j] = row i of A dotted with column j of B. It is not commutative: AB ≠ BA in general. Dimensions must match: A (m×k) times B (k×n) gives C (m×n). A matrix is a linear transformation — it stretches, rotates, and projects vectors. The matrix inverse A⁻¹ satisfies A⁻¹A = I. For linear regression, the normal equations give θ = (X^T X)⁻¹ X^T y — the closed-form solution via the Gram matrix X^T X.

Column space of A: all vectors reachable as Ax for some x. Rank: the dimension of the column space — the number of linearly independent directions A can produce. Rank deficiency means X^T X is singular, meaning the normal equations have no unique solution. You need regularization to fix this.

A norm ‖v‖ measures a vector's length; different norms make different tradeoffs. The L2 (Euclidean) norm ‖v‖₂ = √(∑ᵢ vᵢ²) is the one used in the dot product formula above — for v = [3, 4], ‖v‖₂ = √(9+16) = 5. The L1 norm ‖v‖₁ = ∑ᵢ|vᵢ| sums absolute values (for the same v, 3+4 = 7); minimizing it (Lasso) drives some coefficients to exactly zero, because its penalty grows at a constant rate per coordinate instead of shrinking smoothly toward zero. The L∞ norm ‖v‖∞ = maxᵢ|vᵢ| just takes the largest coordinate. Which norm a loss function penalizes changes what solutions look like: L2 regularization (Ridge, below) shrinks every coefficient a little; L1 regularization (Lasso) zeroes some out entirely.

NOT this. Linear algebra is not just matrix arithmetic for solving linear systems. Every gradient descent step in a neural network is a matrix-vector multiplication. Every embedding lookup is a dot product. PCA is eigenvector decomposition of the covariance matrix. Attention is softmax(QK^T / √d)V — two matrix products (QK^T, then the softmax output times V). The Gram matrix X^T X appears in every regularized linear model. Linear algebra is the operational language of every ML computation, and understanding it geometrically — as transformations of space — is what lets you reason about what information your model is processing.

Key points

Takeaway

Every ML forward pass is matrix multiplication and nonlinearities. Rank tells you where information is irreversibly lost. Norms tell you what geometry an algorithm assumes. Both predict failure modes before you run a single experiment.

Recap

Check your understanding

Q1. A system Ax=b where A is 3×5 (3 equations, 5 unknowns). What can you say about the solution set? When does a solution exist?

Q2. You compute the dot product of two vectors: u·v = ‖u‖‖v‖cos(θ) = 0. What does this mean geometrically, and what does it mean in ML for feature representations?

Q3. The matrix A = [[2, 1], [4, 2]] is singular. Which TWO of the following statements about it are true?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →