Step 3 of 5

Linear Algebra for ML

Six operations that matter - rotation, shear, determinant, eigenvectors, projection, inverse - and where each shows up in ML.

12 min read

Most of machine learning is linear algebra wearing a disguise. Attention is matrix multiplication. Embeddings are vectors, and comparing them is a dot product. PCA is an eigendecomposition. LoRA is a low-rank matrix product. You don't need to be a mathematician - but you do need to see what a matrix does. This guide builds that picture with six operations.

Matrices in motion#

The key mental shift: a matrix is a transformation of space. A 2 × 2 matrix takes every point on the plane and moves it somewhere else - and the whole transformation is determined by just two things: where the basis vectors î = (1, 0) and ĵ = (0, 1) land. The first column of the matrix is where î goes; the second column is where ĵ goes.

[abcd](xy)=x(ac)+y(bd)\begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{pmatrix} x \\ y \end{pmatrix} = x \begin{pmatrix} a \\ c \end{pmatrix} + y \begin{pmatrix} b \\ d \end{pmatrix}

Column 1 = where î lands. Column 2 = where ĵ lands. Every other point follows along, because the transformation is linear.

Here are six transformations. In each, the faint grid is the original space, the violet grid is the transformed space, the shaded shape is where the unit square lands, and the arrows show where î and ĵ go.

Rotation 90°

[[0, -1], [1, 0]] · det 1

Shear

[[1, 1], [0, 1]] · det 1

Scale (det 3)

[[2, 0], [0, 1.5]] · det 3

Reflection (det −1)

[[-1, 0], [0, 1]] · det -1

Projection (det 0)

[[1, 0], [0, 0]] · det 0

Eigen example

[[1.5, 0.5], [0, 1]] · det 1.50

cyan = where î = (1, 0) lands · magenta = where ĵ = (0, 1) lands · shaded = the image of the unit square

1. Rotation - preserves size#

A rotation by angle θ is [[cos θ, −sin θ], [sin θ, cos θ]]. Lengths and angles don't change; only orientation does. Its determinant is 1 and its inverse is its transpose (rotate back by −θ).

In ML: rotary position embeddings (RoPE) rotate query and key vectors by position-dependent angles, so their dot product depends on relative position.

2. Shear - preserves area#

A shear slides one axis along the other, like pushing the top of a deck of cards. The square becomes a parallelogram with the same area - the determinant is 1.

3. Determinant - the area scale factor#

The determinant tells you how much a transformation scales area (volume, in higher dimensions):

detMeaning
det = 3Areas triple
det = 1Area preserved (rotations, shears)
0 < det < 1Areas shrink
det = 0Space is squashed flat - information is lost; no inverse exists
det negativeOrientation flips (like a mirror image)

In ML: a zero determinant means a matrix is singular - the reason you can't naively invert some covariance matrices, and why regularization (adding λI) helps.

4. Eigenvectors - the directions that don't turn#

Most vectors change direction when transformed. Eigenvectors don't: they only get stretched or shrunk by a factor called the eigenvalue λ.

Av=λv\mathbf{A}\mathbf{v} = \lambda \mathbf{v}

v is an eigenvector of A with eigenvalue λ: applying A just scales v.

In the "eigen example" above, [[1.5, 0.5], [0, 1]]: vectors along the x-axis stay on the x-axis, stretched by 1.5 (λ = 1.5), and vectors along (1, −1) stay on that line, unchanged in length (λ = 1). Every other direction gets turned.

In ML: PCA finds the eigenvectors of the data's covariance matrix - the directions of greatest variance - and keeps the top few to compress the data. Eigenvalues also describe the stability of training dynamics and the curvature of loss surfaces.

5. Projection - idempotent#

A projection flattens space onto a line or plane: every point drops to its shadow. Projecting twice is the same as projecting once:

P2=P\mathbf{P}^2 = \mathbf{P}

Idempotent: once you're on the line, projecting again changes nothing. Projections that lose a dimension have determinant 0.

In ML: linear regression's predictions are the projection of the target vector onto the space spanned by the features. Attention heads project token vectors into smaller query, key, and value subspaces.

6. Inverse - undo the transformation#

The inverse A⁻¹ reverses A: A⁻¹A = I (the identity, which changes nothing). It exists only when det A ≠ 0 - you can't un-flatten a squashed space. For a 2 × 2 matrix:

[abcd]−1=1ad−bc[d−b−ca]\begin{bmatrix} a & b \\ c & d \end{bmatrix}^{-1} = \frac{1}{ad - bc} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}

Swap a and d, negate b and c, divide by the determinant - which is why det = 0 means no inverse.

The inverse of the shear [[1, 1], [0, 1]] is [[1, −1], [0, 1]] - slide it back.

In ML: the closed-form least-squares solution is w = (XᵀX)⁻¹Xᵀy. In practice, libraries solve the system rather than computing the inverse explicitly, for speed and numerical stability.

Where it shows up in ML#

Attention is matrix multiplication

QKᵀ computes every query-key dot product at once; multiplying by V mixes the values. A transformer is mostly large matrix multiplies.

Embeddings are vectors

Similarity is a dot product or cosine of the angle between vectors; semantic search is nearest-neighbor search in that space.

PCA is eigendecomposition

Top eigenvectors of the covariance matrix are the principal components - the best low-dimensional summary of the data.

LoRA is low rank

A weight update ΔW = BA with small inner dimension r: a big matrix expressed as the product of two thin ones.

Layers are linear maps + nonlinearity

Each dense layer is Wx + b followed by an activation; stacking them composes transformations.

SVD compresses matrices

Any matrix factors into rotation × scaling × rotation. Keeping the largest singular values gives the best low-rank approximation.

RoPE
Rotary Position Embedding - encodes token position by rotating query and key vectors so attention naturally captures relative distance between tokens.
LoRA
Low-Rank Adaptation - a parameter-efficient fine-tuning method that freezes original weights and trains two small matrices whose product approximates the weight update.
SVD
Singular Value Decomposition - factors any matrix into rotation × scaling × rotation; keeping the top singular values gives the best low-rank approximation.
Cosine similarity
The cosine of the angle between two vectors - 1 means identical direction, 0 means unrelated. The standard way to compare embeddings in semantic search.

Further reading

Search Data Science After Dark

Search across prompts, models, papers, guides, and library topics.