SafeZone AI Learn
Learn/ Mathematical Foundations/ Linear Algebra for ML · lesson 1 of 6

Vectors and linear maps

Vectors as data, inner products as similarity, and the reframe this whole module rides on: a matrix is not a grid of numbers but a transformation of space — one you can grab and bend.

Where this came from. Linear algebra was born three separate times for three unrelated reasons: as elimination (solving simultaneous equations, from Han-dynasty rod arithmetic to Gauss’s astronomy), as transformation (Cayley, 1858, noticing that operations on space could themselves be added and multiplied — matrices as verbs), and as spectrum (eigenvalues, out of vibrating and spinning physics). A century later, data turned out to need all three at once. The full story is in How the mathematics was forged; this module teaches the three births as the one subject they became.

Machine learning is applied linear algebra with a loss function attached. A dataset is a matrix; an embedding is a vector; a layer is a linear map plus a nonlinearity; “similarity” in every retrieval system on Earth is an inner product. This module exists so those aren’t metaphors, and it starts with the one mental upgrade that makes everything else in it easy: stop reading matrices as tables and start reading them as motions of space.

Vectors: data with geometry

A vector xRdx \in \mathbb{R}^d is a point and an arrow — a customer’s features, a word’s embedding, an image’s pixels — and geometry gives data its first useful vocabulary. The norm measures size, and two norms carry this curriculum:

x2=ixi2x1=ixi\lVert x \rVert_2 = \sqrt{\textstyle\sum_i x_i^2} \qquad \lVert x \rVert_1 = \textstyle\sum_i \lvert x_i \rvert

— you met them as ridge and lasso penalties, where their shapes (sphere vs diamond) decided whether coefficients shrink or die. The inner product

x,y=xy=ixiyi=xycosθ\langle x, y \rangle = x^\top y = \textstyle\sum_i x_i y_i = \lVert x \rVert \lVert y \rVert \cos\theta

is the single most-executed mathematical operation on the planet right now, because its geometric reading — how aligned are these two directions? — is what “similarity” means computationally. Cosine similarity (the inner product of normalized vectors) ranks documents against queries in every embedding-based retrieval system; attention scores in transformers are scaled inner products of query and key vectors; even the correlation coefficient of classical statistics is a cosine between centred data columns. When xy=0x^\top y = 0 the directions are orthogonal — carrying no linear information about each other — an idea lesson 4 will build the geometry of least squares and PCA on.

The reframe: a matrix is a transformation

Here is the module’s cornerstone. A matrix ARm×nA \in \mathbb{R}^{m \times n} is the function xAxx \mapsto Ax — and it is completely determined by where it sends the basis vectors, because linearity forces everything else:

Ax=A(x1e1+x2e2)=x1(Ae1)column 1+x2(Ae2)column 2.Ax = A(x_1 e_1 + x_2 e_2) = x_1 \underbrace{(A e_1)}_{\text{column } 1} + x_2 \underbrace{(A e_2)}_{\text{column } 2}.

The columns of AA are the images of the basis vectors, and every output is just a recombination of the columns. Read that sentence until it is obvious; it is the decoder ring for the whole module. Linearity itself is two visual promises — grid lines stay parallel and evenly spaced, the origin stays put — and everything a linear map can do (rotate, stretch, shear, reflect, project, collapse) is a combination of surprisingly few primitives:

Drag Ae₁ and Ae₂ and watch the grid follow — every point in the plane is bound to your two arrows by linearity alone. The shaded parallelogram is the image of the unit square, and the readout’s det A is exactly its signed area: make the arrows nearly parallel and watch the determinant approach zero as the plane crushes toward a line (information being destroyed — no inverse exists); swap the arrows past each other and the sign flips (orientation reversed). Try the presets: rotate preserves all lengths (det = 1), shear slides the plane without changing area, collapse is a genuinely singular matrix — drag a column slightly and watch the plane pop back into two dimensions.

Composition, and why “matrix multiplication” is what it is

Matrix multiplication is function composition: (AB)x=A(Bx)(AB)x = A(Bx), first BB then AA. Every mysterious-seeming rule falls out of that one fact: the row-by-column formula is just “track where the basis goes under both maps”; non-commutativity is obvious (rotate-then-stretch ≠ stretch-then-rotate — try it in the widget); (AB)1=B1A1(AB)^{-1} = B^{-1}A^{-1} is “undo in reverse order,” like socks and shoes. And a deep network before its nonlinearities is exactly a composition WLW2W1xW_L \cdots W_2 W_1 x — which collapses to a single linear map, which is why networks need nonlinearities at all: without them, a hundred layers has exactly the expressive power of one.

Three matrix species recur so often in ML they deserve name-tags now:

SpeciesDefinitionWhere you have already met it
Symmetric, A=AA = A^\topequals its transposecovariance matrices, Hessians, XXX^\top X, kernels
Orthogonal, QQ=IQ^\top Q = Icolumns are orthonormal; pure rotation/reflectionpreserves lengths and inner products — the “safe” transforms inside SVD
Diagonalacts by stretching each axis independentlythe trivially-understood case every decomposition tries to reduce to

The module’s arc, announced in advance: the next two lessons prove that symmetric matrices are secretly diagonal (spectral theorem), and that every matrix is diagonal between two rotations (SVD). Diagonal is the destination; decompositions are the route.

Rank: how much survives the map

The rank of AA is the dimension of its image — how many dimensions of information survive. Full rank preserves everything (invertible, det ≠ 0); rank deficiency crushes some directions to zero, exactly what the collapse preset shows. In ML, rank is a resource dial you have already used: LoRA (RL track) constrains a weight update to rank rr, betting the change a task needs is low-dimensional; the SVD lesson makes that bet precise with the best-possible low-rank approximation theorem, and lesson 4’s projections describe what the lost directions were. And the Supervised Learning module’s collinearity troubles — exploding coefficient variance at 1/(1ρ2)1/(1-\rho^2) — are simply XX approaching rank deficiency: the map almost crushes a direction, so inverting it almost divides by zero.

import numpy as np

A = np.array([[1.4, 0.3],       # columns = images of e1, e2 — same as the widget
              [0.5, 1.1]])
x = np.array([2.0, 1.0])
print(A @ x, np.linalg.det(A))          # transform a point; signed area factor
print(np.linalg.matrix_rank(A))         # dimensions surviving the map

Q, _ = np.linalg.qr(np.random.randn(4, 4))      # a random orthogonal matrix
np.testing.assert_allclose(Q.T @ Q, np.eye(4), atol=1e-12)  # rotations: QᵀQ = I

Exercises

Work these before the next lesson

  1. From “columns are images of basis vectors,” derive the standard row-by-column multiplication formula for a 2×2 product ABAB by tracking e1,e2e_1, e_2 through BB then AA.
    Solution

    Worked solutions are part of Premiumunlock all of them for £5/month →

  2. 5 more exercises — each with a worked solution — are part of Premium. Unlock everything for £5/month →

References

  • G. Strang, Introduction to Linear Algebra, 6th ed. — the column picture, taught by its greatest evangelist.
  • 3Blue1Brown, Essence of Linear Algebra — the animated companion to this lesson’s widget; watch chapters 1–4 alongside it.
  • S. Axler, Linear Algebra Done Right, 4th ed. — the maps-first treatment, free from the author.