Linear algebra is the branch of mathematics that studies vector spaces and linear transformations. Machine learning depends on it at every level: feature vectors live in vector spaces, model parameters are matrices, and algorithms like PCA and gradient descent are defined in terms of eigenvalues, dot products, and matrix decompositions. This page covers the key concepts and their NumPy representations as used in theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/ageron/handson-ml3/llms.txt
Use this file to discover all available pages before exploring further.
math_linear_algebra.ipynb notebook.
Vectors
A vector is a quantity defined by a magnitude and a direction. In ML, vectors represent observations, predictions, and model parameters. Each element (component) of a vector corresponds to one feature or one dimension. For example, a video might be represented by a 4-element vector:Vector norm
The Euclidean norm (or L2 norm) measures the length of a vector:Vector addition and scalar multiplication
u + v == v + u) and associative. Adding a vector to a set of points translates every point by that vector — a key geometric operation.
Dot product
The dot product of two vectors produces a scalar. It measures how much two vectors point in the same direction:X @ w), similarity scores in nearest-neighbour search, and the operations in every neural network layer.
Matrices
A matrix is a 2D rectangular array of scalars. In NumPy a matrix is just a rank-2ndarray:
Matrix addition and scalar multiplication
Transpose
Swapping rows and columns:Matrix multiplication
A matrix of shape(m, n) can multiply a matrix of shape (n, q), giving a result of shape (m, q). Each element P[i, j] is the dot product of row i from the first matrix and column j from the second:
A @ D ≠ D @ A in general.
The @ operator also computes dot products between vectors:
Matrix inverse
The inverseA⁻¹ of a square matrix A satisfies A @ A⁻¹ = I (the identity matrix). It exists only for non-singular (full-rank) matrices:
A x = b using LA.solve(A, b) is preferred over explicit inversion — it is numerically more stable.
Eigenvalues and eigenvectors
For a square matrixA, a non-zero vector v is an eigenvector if A @ v = λ v for some scalar λ called the eigenvalue. Eigenvalues capture how much the matrix stretches or compresses space along each eigenvector direction.
Singular Value Decomposition (SVD)
SVD generalises eigendecomposition to non-square matrices. Any matrixA (shape m × n) can be factorised as:
U is m × m orthogonal, s is a vector of non-negative singular values, and Vt is n × n orthogonal.
np.linalg.svd is called internally by sklearn.decomposition.PCA), low-rank approximations, and pseudo-inverse computations.
Identity and zero matrices
Summary of key NumPy linalg functions
| Function | Purpose |
|---|---|
LA.norm(v) | Vector (or matrix) norm |
LA.dot(A, B) or A @ B | Dot product / matrix multiply |
LA.inv(A) | Matrix inverse |
LA.solve(A, b) | Solve linear system Ax = b |
LA.det(A) | Determinant |
LA.eig(A) | Eigenvalues and eigenvectors |
LA.svd(A) | Singular value decomposition |
LA.matrix_rank(A) | Rank of a matrix |