solulubeta
Sign in
Machine Learning / Math foundations
01.02 · Lesson

Linear Algebra for ML

Vectors, dot products, matrices, matrix multiplication, transposes, inverses, eigenvectors and why a dataset is just a matrix.

3 min Quiz Free

Machine learning runs on linear algebra. A dataset is a matrix, a model’s weights are a matrix, and a prediction is a matrix multiplication. You need a working feel for a handful of ideas.

Vectors

A vector is a list of numbers, for example a house described by its features:

x=[12003215](sq.ft, bedrooms, bathrooms, age)\mathbf{x} = \begin{bmatrix} 1200 \\ 3 \\ 2 \\ 15 \end{bmatrix} \quad (\text{sq.ft, bedrooms, bathrooms, age})

Geometrically it’s a point (or an arrow) in nn-dimensional space. Key operations:

  • Addition and scaling, element by element.
  • Norm (length): ∥x∥2=∑ixi2\lVert \mathbf{x} \rVert_2 = \sqrt{\sum_i x_i^2}. The L1 norm ∥x∥1=∑i∣xi∣\lVert \mathbf{x} \rVert_1 = \sum_i |x_i| appears in Lasso regularisation.
  • Dot product: a⋅b=∑iaibi=∥a∥ ∥b∥cos⁡θ\mathbf{a} \cdot \mathbf{b} = \sum_i a_i b_i = \lVert\mathbf{a}\rVert\,\lVert\mathbf{b}\rVert \cos\theta.

The dot product is everywhere. A linear model’s prediction is a dot product of weights and features:

y^=w⋅x+b=w1x1+w2x2+⋯+wnxn+b\hat{y} = \mathbf{w} \cdot \mathbf{x} + b = w_1 x_1 + w_2 x_2 + \dots + w_n x_n + b

Cosine similarity, a⋅b∥a∥∥b∥\frac{\mathbf{a}\cdot\mathbf{b}}{\lVert\mathbf{a}\rVert\lVert\mathbf{b}\rVert}, measures how aligned two vectors are regardless of length. It is the standard way to compare embeddings (word, sentence and image vectors) in search and recommendations.

Matrices

A matrix is a grid of numbers. A dataset with mm examples and nn features is an m×nm \times n matrix XX: row ii is example ii.

Matrix–vector multiplication applies a linear model to every example at once:

y^=Xw+b(m×n)(n×1)=(m×1)\hat{\mathbf{y}} = X\mathbf{w} + b \qquad (m \times n)(n \times 1) = (m \times 1)

Matrix multiplication C=ABC = AB: entry CijC_{ij} is the dot product of row ii of AA and column jj of BB. Shapes must line up: (m×n)(n×p)=(m×p)(m \times n)(n \times p) = (m \times p). It is not commutative: AB≠BAAB \neq BA in general.

A neural network layer is exactly this: h=σ(Wx+b)\mathbf{h} = \sigma(W\mathbf{x} + \mathbf{b}). GPUs exist in ML because they do huge matrix multiplications fast.

Transpose, identity, inverse

  • Transpose A⊤A^\top: flip rows and columns. (AB)⊤=B⊤A⊤(AB)^\top = B^\top A^\top.
  • Identity II: ones on the diagonal; AI=AAI = A.
  • Inverse A−1A^{-1}: AA−1=IAA^{-1} = I. It exists only for square matrices with non-zero determinant.

These give the closed-form solution of linear regression (the normal equation):

w∗=(X⊤X)−1X⊤y\mathbf{w}^* = (X^\top X)^{-1} X^\top \mathbf{y}

In practice we rarely invert matrices (it is slow and numerically unstable); we use gradient descent or solvers. The formula is still a classic interview question.

Eigenvectors and eigenvalues

For a square matrix AA, an eigenvector v\mathbf{v} only gets scaled (not rotated) by AA:

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

λ\lambda is its eigenvalue. Why ML cares: in PCA, the eigenvectors of the data’s covariance matrix are the directions of greatest variance, and their eigenvalues say how much variance each captures. Keeping the top few compresses the data with minimal information loss.

SVD (singular value decomposition) generalises this to any matrix, A=UΣV⊤A = U\Sigma V^\top. It powers PCA, recommendation systems (matrix factorisation) and low-rank tricks like LoRA fine-tuning.

Shapes: the habit that saves hours

Most ML bugs are shape bugs. Write shapes next to every expression:

ObjectShape
Data XXm×nm \times n (examples × features)
Weights w\mathbf{w}n×1n \times 1
Predictions XwX\mathbf{w}m×1m \times 1
Layer weights WWout×in\text{out} \times \text{in}
A batch of imagesbatch × channels × height × width

Check yourself

Question 1 of 3

A dataset has 1,000 examples with 20 features each. As a matrix X it has shape…

Question 2 of 3

If A is 3×4 and B is 4×2, what is the shape of AB?

Question 3 of 3

The dot product of two vectors is zero. The vectors are…

Mark this as done when you are finished.

Item 2 of 24 · updated 2026-10-12