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:
- Linear Algebra & Matrix Theory for row reduction, determinants and eigenvalues in depth.
- Calculus Essentials (especially partial derivatives and the chain rule) and Calculus in Depth.
- Probability Theory and Statistics for the probability background.
- NumPy for arrays, and Machine Learning for the scikit-learn side of the same algorithms.
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.
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 is orthogonal to every column of .
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 (, , ), 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 @ thetamultiplies a matrix by a vector either way;X.Ttransposes. When shapes confuse you, print.shapefirst.
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 , and what is the shape of ?
Answer
is ; is .
Solution
Rows are examples and columns are features, so . Then is , and a product is : one entry per pair of features, independent of how many examples there are.
Exercise 2: Read the fit
With intercept and slope from the first example, predict at .
Answer
.
Solution
Exercise 3: A perfect fit (code)
Fit a line to the points , , with the normal equations. What do you get, and why are all residuals zero?
Answer
Intercept 3, slope 2; the points lie exactly on .
Solution
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 is already a combination of the columns of (), the projection of onto the column space is itself.
Test yourself
In this course, a dataset of examples with features is usually stored as…
- a matrix
- an matrix, one example per row
- a single long vector
- a tensor
Which of these is a matrix decomposition used by PCA?
- The singular value decomposition
- Long division
- The Fourier series
- The binomial theorem
Training most ML models means…
- solving one equation by hand
- minimizing a loss function, usually with gradient-based optimization
- sorting the data
- computing a determinant