AI-210 · Lesson 1 of 12 · 35 min
Vectors, matrices and decompositions
Machine learning is written in the language of linear algebra. A data point is a vector of features; a data set is a matrix with one row per example; a neural network layer is a matrix that transforms one vector into another; a word, an image or a user is represented as an embedding, a vector whose geometry captures meaning. Training adjusts matrices, and many of the most useful techniques, from principal component analysis to the low-rank adapters used to fine-tune large language models, are applications of matrix decompositions.
This course gathers the mathematics that machine learning uses most: linear algebra, matrix calculus, probability, information theory, optimization and learning theory, starting with linear algebra in this lesson. It assumes first courses in linear algebra, calculus and statistics, and focuses on the ideas as machine learning applies them. Each lesson includes short NumPy programs (the tools from AI-110) so that every idea can be computed and checked.
Vectors
A vector \(\mathbf{x} \in \mathbb{R}^n\) is an ordered list of \(n\) real numbers. In machine learning it usually represents one example: a house described by its area, age and number of rooms; a 28 × 28 image flattened into 784 pixel values; or a word represented by a learned embedding of a few hundred to a few thousand dimensions. Geometrically, a vector is a point, or an arrow from the origin, in \(n\)-dimensional space.
Dot products, norms and angles
The dot product of two vectors is the sum of the products of their components:
The Euclidean length, or \(\ell_2\) norm, is \(\|\mathbf{x}\|_2 = \sqrt{\mathbf{x}^\top\mathbf{x}}\). Other norms appear too: the \(\ell_1\) norm \(\|\mathbf{x}\|_1 = \sum |x_i|\), which encourages sparse solutions when used as a penalty, and the \(\ell_\infty\) norm, the largest absolute component. The dot product also measures the angle \(\theta\) between vectors:
This cosine similarity is the standard way to compare embeddings: it is 1 for vectors pointing the same way, 0 for perpendicular (orthogonal) vectors and −1 for opposite ones, regardless of their lengths. Search engines, recommendation systems and retrieval-augmented language models all find "similar" items by searching for vectors with high cosine similarity.
import numpy as np
# Toy 4-dimensional "embeddings" of three words
king = np.array([0.8, 0.6, 0.1, 0.9])
queen = np.array([0.7, 0.7, 0.2, 0.8])
apple = np.array([0.1, 0.2, 0.9, 0.1])
def cosine(a, b):
return a @ b / (np.linalg.norm(a) * np.linalg.norm(b))
print(f"|king| = {np.linalg.norm(king):.3f}")
print(f"cos(king, queen) = {cosine(king, queen):.3f}")
print(f"cos(king, apple) = {cosine(king, apple):.3f}")|king| = 1.349 cos(king, queen) = 0.990 cos(king, apple) = 0.302
Worked problem
Projection onto a direction
Find the angle between \(\mathbf{a} = (3, 4)\) and \(\mathbf{b} = (1, 0)\), and the projection of \(\mathbf{a}\) onto \(\mathbf{b}\).
Show solution
- \(\mathbf{a}^\top\mathbf{b} = 3\); \(\|\mathbf{a}\| = 5\); \(\|\mathbf{b}\| = 1\).
- \(\cos\theta = 3/5 = 0.6\), so \(\theta = 53.1°\).
- Projection of \(\mathbf{a}\) onto \(\mathbf{b}\): \(\dfrac{\mathbf{a}^\top\mathbf{b}}{\mathbf{b}^\top\mathbf{b}}\,\mathbf{b} = 3\,(1, 0) = (3, 0)\).
Answer: The angle is 53.1° and the projection is (3, 0). Projection, the component of a vector along a direction, is the operation behind least squares and principal component analysis.
Matrices as linear maps
A matrix \(\mathbf{A} \in \mathbb{R}^{m \times n}\) is a rectangular array of numbers, and multiplying it by a vector, \(\mathbf{y} = \mathbf{A}\mathbf{x}\), maps a vector in \(\mathbb{R}^n\) to one in \(\mathbb{R}^m\). Every linear map can be written this way: rotations, scalings, reflections, projections and their combinations. Each output component is the dot product of a row of \(\mathbf{A}\) with \(\mathbf{x}\); equivalently, \(\mathbf{A}\mathbf{x}\) is a weighted sum of the columns of \(\mathbf{A}\), with weights given by \(\mathbf{x}\).
In a data matrix \(\mathbf{X} \in \mathbb{R}^{N \times d}\), each of the \(N\) rows is an example and each of the \(d\) columns is a feature. A linear model predicts \(\hat{\mathbf{y}} = \mathbf{X}\mathbf{w}\) for all examples at once, and a neural network layer computes \(\mathbf{H} = \sigma(\mathbf{X}\mathbf{W} + \mathbf{b})\), applying a nonlinearity \(\sigma\) element by element. The product of two matrices is the composition of their maps; it is associative but not commutative: in general \(\mathbf{A}\mathbf{B} \ne \mathbf{B}\mathbf{A}\).
Some special matrices recur constantly. The identity \(\mathbf{I}\) leaves vectors unchanged. The transpose \(\mathbf{A}^\top\) swaps rows and columns, with \((\mathbf{A}\mathbf{B})^\top = \mathbf{B}^\top\mathbf{A}^\top\). A symmetric matrix equals its transpose; covariance matrices and Hessians are symmetric. An orthogonal matrix \(\mathbf{Q}\) has orthonormal columns, so \(\mathbf{Q}^\top\mathbf{Q} = \mathbf{I}\): it rotates or reflects without changing lengths. A diagonal matrix scales each coordinate separately.
Rank, inverses and linear systems
The rank of a matrix is the number of linearly independent columns (equivalently rows): the dimension of the space its outputs can reach. An \(n \times n\) matrix of rank \(n\) is invertible: there is a matrix \(\mathbf{A}^{-1}\) with \(\mathbf{A}^{-1}\mathbf{A} = \mathbf{I}\), and the system \(\mathbf{A}\mathbf{x} = \mathbf{b}\) has exactly one solution. A matrix is invertible if and only if its determinant is non-zero. In numerical work, systems are solved with factorizations (np.linalg.solve) rather than by forming the inverse, which is slower and less accurate.
Worked problem
Solving a 2 × 2 system
Solve \(2x_1 + x_2 = 5\), \(x_1 + 3x_2 = 10\) using the inverse.
Show solution
- \(\mathbf{A} = \begin{bmatrix} 2 & 1 \ 1 & 3 \end{bmatrix}\), \(\det\mathbf{A} = 6 - 1 = 5\).
- \(\mathbf{A}^{-1} = \tfrac{1}{5}\begin{bmatrix} 3 & -1 \ -1 & 2 \end{bmatrix}\).
- \(\mathbf{x} = \mathbf{A}^{-1}\mathbf{b} = \tfrac{1}{5}(3 \times 5 - 10,\; -5 + 2 \times 10) = \tfrac{1}{5}(5, 15) = (1, 3)\).
Answer: \(x_1 = 1\), \(x_2 = 3\). Check: \(2 + 3 = 5\) and \(1 + 9 = 10\).
Eigenvalues and eigenvectors
An eigenvector of a square matrix \(\mathbf{A}\) is a non-zero vector whose direction the matrix does not change: it only scales it, by the eigenvalue \(\lambda\):
Eigenvalues are the roots of the characteristic equation \(\det(\mathbf{A} - \lambda\mathbf{I}) = 0\). Symmetric matrices have a particularly clean structure, given by the spectral theorem: all their eigenvalues are real, and their eigenvectors can be chosen orthonormal. A symmetric matrix can therefore be written \(\mathbf{A} = \mathbf{Q}\boldsymbol{\Lambda}\mathbf{Q}^\top\), where the columns of \(\mathbf{Q}\) are eigenvectors and \(\boldsymbol{\Lambda}\) is the diagonal matrix of eigenvalues. Geometrically, it stretches space by \(\lambda_i\) along each eigenvector direction.
Worked problem
Eigenvalues of a 2 × 2 symmetric matrix
Find the eigenvalues and eigenvectors of \(\mathbf{A} = \begin{bmatrix} 2 & 1 \ 1 & 2 \end{bmatrix}\).
Show solution
- \(\det(\mathbf{A} - \lambda\mathbf{I}) = (2 - \lambda)^2 - 1 = 0\), so \(2 - \lambda = \pm 1\): \(\lambda = 1\) or \(\lambda = 3\).
- For \(\lambda = 3\): \((\mathbf{A} - 3\mathbf{I})\mathbf{v} = 0\) gives \(-v_1 + v_2 = 0\), so \(\mathbf{v} \propto (1, 1)\).
- For \(\lambda = 1\): \(v_1 + v_2 = 0\), so \(\mathbf{v} \propto (1, -1)\).
- Normalized: \((1, 1)/\sqrt2\) and \((1, -1)/\sqrt2\), which are orthogonal, as the spectral theorem promises.
Answer: Eigenvalues 3 and 1, with eigenvectors along (1, 1) and (1, −1). The matrix stretches space by 3 along the diagonal and leaves the perpendicular direction unchanged.
import numpy as np
A = np.array([[2.0, 1.0],
[1.0, 2.0]])
vals, vecs = np.linalg.eigh(A) # eigh: for symmetric matrices
vecs = vecs * np.sign(vecs[0]) # fix signs (they are arbitrary)
print("eigenvalues:", vals)
print("eigenvectors (columns):")
print(vecs.round(3))
v = vecs[:, 1]
print("A v =", (A @ v).round(3), " lambda v =", (vals[1] * v).round(3))eigenvalues: [1. 3.] eigenvectors (columns): [[ 0.707 0.707] [-0.707 0.707]] A v = [2.121 2.121] lambda v = [2.121 2.121]
Eigenvalues appear all over machine learning. The eigenvalues of a covariance matrix measure how much the data varies in each direction (the basis of PCA, below). The eigenvalues of a loss function's Hessian describe its curvature, which controls how fast gradient descent converges (Lesson 11). A symmetric matrix whose eigenvalues are all positive is positive definite: \(\mathbf{x}^\top\mathbf{A}\mathbf{x} > 0\) for every non-zero \(\mathbf{x}\). Covariance matrices are always positive semidefinite (eigenvalues ≥ 0).
The singular value decomposition
Eigendecomposition needs a square (and ideally symmetric) matrix. The singular value decomposition (SVD) works for any matrix, and it is arguably the most useful factorization in data science. Every \(m \times n\) matrix can be written
where \(\mathbf{U}\) (\(m \times m\)) and \(\mathbf{V}\) (\(n \times n\)) are orthogonal and \(\boldsymbol{\Sigma}\) is diagonal with non-negative singular values \(\sigma_1 \ge \sigma_2 \ge \dots \ge 0\). Geometrically, any linear map is a rotation (\(\mathbf{V}^\top\)), a scaling along the axes (\(\boldsymbol{\Sigma}\)) and another rotation (\(\mathbf{U}\)). The number of non-zero singular values is the rank. The singular values of \(\mathbf{A}\) are the square roots of the eigenvalues of \(\mathbf{A}^\top\mathbf{A}\).
Low-rank approximation
Keeping only the \(k\) largest singular values and their vectors gives \(\mathbf{A}_k = \mathbf{U}_k\boldsymbol{\Sigma}_k\mathbf{V}_k^\top\). By the Eckart–Young theorem, this is the best possible rank-\(k\) approximation of \(\mathbf{A}\), and its error in the Frobenius norm (the square root of the sum of all squared entries) is exactly the square root of the sum of the discarded squared singular values:
import numpy as np
rng = np.random.default_rng(0)
# A 6 x 5 matrix that is nearly rank 2, plus a little noise
U = rng.normal(size=(6, 2)); V = rng.normal(size=(2, 5))
M = U @ V + 0.05 * rng.normal(size=(6, 5))
s = np.linalg.svd(M, compute_uv=False)
print("singular values:", s.round(3))
Uf, sf, Vt = np.linalg.svd(M, full_matrices=False)
M2 = Uf[:, :2] @ np.diag(sf[:2]) @ Vt[:2] # best rank-2 approximation
err = np.linalg.norm(M - M2) # Frobenius norm of the error
print(f"rank-2 error {err:.4f} = sqrt(sum of the other s^2) {np.sqrt((sf[2:]**2).sum()):.4f}")
print(f"relative error {err / np.linalg.norm(M):.4f}")singular values: [5.054 2.148 0.116 0.079 0.024] rank-2 error 0.1424 = sqrt(sum of the other s^2) 0.1424 relative error 0.0259
The singular values drop sharply after the second, revealing that the matrix is essentially rank 2 plus noise, and the rank-2 approximation captures it to within about 2.6%. Real data matrices (user-by-item ratings, word co-occurrence counts, images) often behave like this, which is why low-rank methods compress them so well and why recommendation systems based on matrix factorization work.
Worked problem
Low-rank adaptation of a large model
A layer of a language model has a 4096 × 4096 weight matrix. Fine-tuning the full matrix updates every entry. Low-rank adaptation (LoRA) instead learns an update \(\Delta\mathbf{W} = \mathbf{B}\mathbf{A}\) with \(\mathbf{B}\) of size 4096 × 8 and \(\mathbf{A}\) of size 8 × 4096. Compare the number of trainable parameters.
Show solution
- Full matrix: \(4096^2 = 16{,}777{,}216\) parameters.
- LoRA: \(4096 \times 8 + 8 \times 4096 = 65{,}536\) parameters.
- Ratio: \(65{,}536/16{,}777{,}216 = 0.39\%\).
- The update \(\mathbf{B}\mathbf{A}\) has rank at most 8.
Answer: LoRA trains about 0.4% as many parameters, on the bet that the change needed for a new task is approximately low-rank. It works remarkably well in practice and is one of the standard ways to fine-tune large models on modest hardware.
Principal component analysis
Principal component analysis (PCA) finds the directions in which data varies most. For a data matrix \(\mathbf{X}\) whose columns have been centred (mean subtracted), the covariance matrix is \(\mathbf{C} = \mathbf{X}^\top\mathbf{X}/(N - 1)\). Its eigenvectors are the principal directions, and its eigenvalues are the variances along them. In practice PCA is computed from the SVD of the centred data, \(\mathbf{X} = \mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top\): the rows of \(\mathbf{V}^\top\) are the principal directions, and the variance along direction \(i\) is \(\sigma_i^2/(N - 1)\). The fraction of total variance explained by component \(i\) is \(\sigma_i^2/\sum_j \sigma_j^2\).
Projecting onto the first \(k\) principal components, \(\mathbf{Z} = \mathbf{X}\mathbf{V}_k\), reduces each example from \(d\) numbers to \(k\) while keeping as much variance as possible. PCA is used for visualization (projecting to two dimensions), compression, noise reduction and as a preprocessing step for other models.
import numpy as np
rng = np.random.default_rng(1)
# 500 points in 3-D that mostly vary along one direction
t = rng.normal(size=500)
X = np.column_stack([2 * t, 1 * t, 0.5 * t]) + 0.3 * rng.normal(size=(500, 3))
Xc = X - X.mean(axis=0) # subtract the mean of each column
U, s, Vt = np.linalg.svd(Xc, full_matrices=False)
explained = s**2 / (s**2).sum()
pc1 = Vt[0] * np.sign(Vt[0, 0]) # fix the arbitrary sign
print("explained variance ratio:", explained.round(3))
print("first principal direction:", pc1.round(3))
Z = Xc @ pc1 # project onto 1 component
print("projected shape:", Z.shape)explained variance ratio: [0.96 0.022 0.018] first principal direction: [0.87 0.442 0.219] projected shape: (500,)
The first principal component explains 96% of the variance, and its direction (0.87, 0.44, 0.22) is close to the normalized generating direction (2, 1, 0.5)/2.29 = (0.87, 0.44, 0.22). One number per point now describes almost all of the structure in three.
How this course is organized
The course moves from linear algebra through calculus and probability to learning:
- Lessons 2 and 3 continue with linear algebra: least squares and projections, then high-dimensional geometry, embeddings and kernels.
- Lessons 4 and 5 cover matrix calculus and backpropagation, then automatic differentiation and numerical precision.
- Lessons 6 to 8 cover probability for learning, the multivariate Gaussian and Gaussian processes, and generalization theory.
- Lesson 9 covers information theory and Lesson 10 the linear algebra of deep learning.
- Lesson 11 covers convexity and optimization, and Lesson 12 builds logistic regression and a neural network from first principles.
Common mistakes
MistakeAssuming \(\mathbf{A}\mathbf{B} = \mathbf{B}\mathbf{A}\).
InsteadMatrix multiplication is not commutative; the order of transformations matters.
MistakeComputing \(\mathbf{A}^{-1}\mathbf{b}\) by forming the inverse.
InsteadUse a solver (np.linalg.solve), which is faster and numerically more accurate.
MistakeComparing embeddings with the raw dot product when their lengths differ.
InsteadUse cosine similarity, or normalize the vectors first.
MistakeRunning PCA on data that has not been centred, or on features with very different scales.
InsteadSubtract the mean of each feature, and standardize unless the units are comparable.
MistakeExpecting eigenvector or singular vector signs to be the same in every library.
InsteadSigns are arbitrary; fix a convention (for example a positive first component) when you need reproducible output.
Summary and key terms
- Examples, embeddings and parameters are vectors; data sets and layers are matrices.
- Dot product \(\mathbf{x}^\top\mathbf{y}\), norms \(\ell_1, \ell_2, \ell_\infty\), and cosine similarity for comparing directions.
- A matrix is a linear map; \(\mathbf{A}\mathbf{x}\) combines the columns of \(\mathbf{A}\) with weights \(\mathbf{x}\); products compose maps and do not commute.
- Rank counts independent columns; full-rank square matrices are invertible; solve systems with factorizations.
- Eigenvectors satisfy \(\mathbf{A}\mathbf{v} = \lambda\mathbf{v}\); symmetric matrices have real eigenvalues and orthonormal eigenvectors, \(\mathbf{A} = \mathbf{Q}\boldsymbol{\Lambda}\mathbf{Q}^\top\).
- The SVD \(\mathbf{A} = \mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top\) exists for every matrix; truncating it gives the best low-rank approximation.
- Low-rank structure underlies recommendation systems, compression and LoRA fine-tuning.
- PCA: SVD of centred data; principal directions in \(\mathbf{V}\), explained variance \(\sigma_i^2/\sum\sigma_j^2\).
- Vector
- An ordered list of numbers, representing a point or direction in \(\mathbb{R}^n\).
- Embedding
- A learned vector representation of an item such as a word, image or user.
- Norm
- A measure of vector length, such as the \(\ell_2\) (Euclidean) or \(\ell_1\) norm.
- Cosine similarity
- The cosine of the angle between two vectors, from −1 to 1.
- Linear map
- A function that preserves addition and scaling, represented by a matrix.
- Transpose
- The matrix obtained by swapping rows and columns.
- Orthogonal matrix
- A square matrix with orthonormal columns, so \(\mathbf{Q}^\top\mathbf{Q} = \mathbf{I}\).
- Rank
- The number of linearly independent columns of a matrix.
- Eigenvector
- A non-zero vector that a matrix only scales: \(\mathbf{A}\mathbf{v} = \lambda\mathbf{v}\).
- Positive definite
- A symmetric matrix with \(\mathbf{x}^\top\mathbf{A}\mathbf{x} > 0\) for all non-zero \(\mathbf{x}\).
- Singular value decomposition
- The factorization \(\mathbf{A} = \mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^\top\) of any matrix.
- Frobenius norm
- The square root of the sum of squared entries of a matrix.
- Low-rank approximation
- Approximating a matrix by one of lower rank, best done by truncating the SVD.
- Principal component analysis
- Finding orthogonal directions of maximum variance in data.
- LoRA
- Low-rank adaptation: fine-tuning a model by learning low-rank updates to its weight matrices.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Compute the \(\ell_1\), \(\ell_2\) and \(\ell_\infty\) norms of \((3, -4, 12)\).
Show answer
ℓ₁ = 19; ℓ₂ = √(9 + 16 + 144) = 13; ℓ∞ = 12.
Find the cosine similarity of \((1, 2, 2)\) and \((2, 0, 1)\).
Show answer
Dot = 4; norms 3 and √5; cos = 4/(3√5) = 0.596.
If \(\mathbf{X}\) is 1000 × 20 and \(\mathbf{W}\) is 20 × 64, what is the shape of \(\mathbf{X}\mathbf{W}\), and what does each row represent?
Show answer
1000 × 64: each row is one example's 64-dimensional representation after the layer.
Find the inverse of \(\begin{bmatrix} 4 & 2 \ 1 & 1 \end{bmatrix}\).
Show answer
det = 2; inverse = ½[[1, −2], [−1, 4]] = [[0.5, −1], [−0.5, 2]].
Find the eigenvalues of \(\begin{bmatrix} 5 & 2 \ 2 & 2 \end{bmatrix}\).
Show answer
(5 − λ)(2 − λ) − 4 = λ² − 7λ + 6 = 0, so λ = 6 and 1.
A 3 × 3 matrix has singular values 10, 3 and 0. What is its rank, and what is the Frobenius error of its best rank-1 approximation?
Show answer
Rank 2; error = √(3² + 0²) = 3.
A matrix has singular values 8, 4, 2, 1. What fraction of \(\|\mathbf{A}\|_F^2\) does the rank-2 approximation keep?
Show answer
(64 + 16)/(64 + 16 + 4 + 1) = 80/85 = 94.1%.
How many parameters does a rank-16 LoRA update of a 1024 × 4096 matrix have?
Show answer
1024 × 16 + 16 × 4096 = 16,384 + 65,536 = 81,920 (about 2% of 4,194,304).
Centred data has singular values 30, 10 and 5. What fraction of variance do the first two components explain?
Show answer
(900 + 100)/(900 + 100 + 25) = 1000/1025 = 97.6%.
Why are the eigenvalues of a covariance matrix never negative?
Show answer
C = XᵀX/(N − 1), so vᵀCv = ‖Xv‖²/(N − 1) ≥ 0 for every v; the variance along any direction cannot be negative.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.