TExcelTAD
Home › Math for Machine Learning › Math for ML Intro

Math for ML Intro

Machine learning libraries hide a lot of mathematics behind one-line calls like model.fit(X, y). This course opens that box. You will see why a dataset is a matrix, why PCA is an eigenvalue problem, why training is "follow the negative gradient", why a Gaussian shows up everywhere and why an SVM only cares about a handful of points, and you will compute each of these yourself in NumPy.

The reference book

The topic order of this course follows the chapters of Mathematics for Machine Learning by Marc Peter Deisenroth, A. Aldo Faisal and Cheng Soon Ong (Cambridge University Press, 2020). The authors publish a free PDF of the book at mml-book.github.io. Thanks to them for making it openly available.

The book is used only as a map of topics. All explanations, numbers, code and exercises in these lessons are written from scratch for this site, with a programmer's angle and runnable Python.

What is in the course

Part Topics
1. Linear algebra for ML data as matrices, linear systems, vector spaces, basis and rank, linear and affine maps
2. Analytic geometry norms, inner products, angles, orthogonal projections, rotations
3. Matrix decompositions determinant and trace, eigenvalues, Cholesky, eigendecomposition, SVD, low-rank approximation
4. Vector calculus Taylor series, gradients, Jacobians, matrix derivatives, backpropagation, Hessians, gradient checks
5. Probability random variables, densities, Bayes, expectation, covariance, the Gaussian, exponential families, change of variables
6. Continuous optimization gradient descent, momentum, SGD, Lagrange multipliers, convexity, linear and quadratic programs
7. When models meet data empirical risk, maximum likelihood, MAP and regularization, model selection
8. Applications linear regression, PCA, Gaussian mixtures with EM, support vector machines

Parts 1 to 6 are the toolbox; part 7 says how the toolbox turns data into a model; part 8 derives four classic algorithms from it.

What you should know first

This is an advanced course. It reviews each topic quickly with ML in mind, and links to the full treatment elsewhere on the site instead of repeating it:

Here is the whole course in one small example: fit a line to data by solving a linear system, which you will later recognize as least squares, maximum likelihood and an orthogonal projection all at once.

python · try it yourself
import numpy as np

x = np.array([0.0, 1.0, 2.0, 3.0, 4.0])
y = np.array([1.1, 2.9, 5.2, 6.8, 9.1])
X = np.column_stack([np.ones_like(x), x])     # design matrix: a column of 1s and the inputs
theta = np.linalg.solve(X.T @ X, X.T @ y)     # normal equations
print("intercept, slope:", np.round(theta, 4))
print("prediction at x = 5:", round(float(theta @ [1, 5]), 4))
intercept, slope: [1.04 1.99]
prediction at x = 5: 10.99

And the same matrix language doing geometry: the residual y−Xθ\mathbf{y} - X\boldsymbol{\theta} is orthogonal to every column of XX.

python · try it yourself
import numpy as np

x = np.array([0.0, 1.0, 2.0, 3.0, 4.0])
y = np.array([1.1, 2.9, 5.2, 6.8, 9.1])
X = np.column_stack([np.ones_like(x), x])
theta = np.linalg.solve(X.T @ X, X.T @ y)
r = y - X @ theta
print("residual:", np.round(r, 4))
print("X^T r:", np.round(X.T @ r, 10) + 0)
residual: [ 0.06 -0.13  0.18 -0.21  0.1 ]
X^T r: [0. 0.]

How lessons work

Each lesson explains one idea, writes its formulas in math notation (∇θL\nabla_{\boldsymbol{\theta}} L, E[X]\mathbb{E}[X], A=UΣVTA = U\Sigma V^T), computes it in NumPy, and ends with an In software note on where libraries use it, exercises with answers and worked solutions, and a short quiz. Only NumPy (and the Python standard library) is used, so everything runs in your browser.

Tip: Vectors are columns in the formulas but 1-D arrays in NumPy. X @ theta multiplies a matrix by a vector either way; X.T transposes. When shapes confuse you, print .shape first.

Learning paths

Your goal Suggested route
AI / ML engineer Calculus → Linear Algebra → Probability → this course → Optimization → Machine Learning → the Machine Learning track
Deep learning and LLMs this course (parts 1, 4, 5, 6) → Math for Deep Learning & LLMs → Generative AI & LLMs
Data scientist Statistics → this course (parts 3, 5, 7, 8) → Data Science

Exercises

Exercise 1: Shapes

A dataset has 500 examples with 12 features each, stored one example per row. What is the shape of the data matrix XX, and what is the shape of XTXX^T X?

Answer

XX is 500×12500 \times 12; XTXX^T X is 12×1212 \times 12.

Solution

Rows are examples and columns are features, so X∈R500×12X \in \mathbb{R}^{500 \times 12}. Then XTX^T is 12×50012 \times 500, and a (12×500)(500×12)(12 \times 500)(500 \times 12) product is 12×1212 \times 12: one entry per pair of features, independent of how many examples there are.

Exercise 2: Read the fit

With intercept 1.041.04 and slope 1.991.99 from the first example, predict yy at x=10x = 10.

Answer

20.9420.94.

Solution
y^=θ0+θ1x=1.04+1.99⋅10=20.94\hat{y} = \theta_0 + \theta_1 x = 1.04 + 1.99 \cdot 10 = 20.94

Exercise 3: A perfect fit (code)

Fit a line to the points (0,3)(0, 3), (1,5)(1, 5), (2,7)(2, 7) with the normal equations. What do you get, and why are all residuals zero?

Answer

Intercept 3, slope 2; the points lie exactly on y=3+2xy = 3 + 2x.

Solution
python · try it yourself
import numpy as np
x = np.array([0.0, 1.0, 2.0]); y = np.array([3.0, 5.0, 7.0])
X = np.column_stack([np.ones_like(x), x])
theta = np.linalg.solve(X.T @ X, X.T @ y)
print(np.round(theta, 6) + 0, np.round(y - X @ theta, 6) + 0)
[3. 2.] [0. 0. 0.]

Since y\mathbf{y} is already a combination of the columns of XX (y=3⋅1+2x\mathbf{y} = 3 \cdot \mathbf{1} + 2\mathbf{x}), the projection of y\mathbf{y} onto the column space is y\mathbf{y} itself.

Test yourself

In this course, a dataset of NN examples with DD features is usually stored as…

  1. a D×DD \times D matrix
  2. an N×DN \times D matrix, one example per row
  3. a single long vector
  4. a D×N×ND \times N \times N tensor

Which of these is a matrix decomposition used by PCA?

  1. The singular value decomposition
  2. Long division
  3. The Fourier series
  4. The binomial theorem

Training most ML models means…

  1. solving one equation by hand
  2. minimizing a loss function, usually with gradient-based optimization
  3. sorting the data
  4. computing a determinant