Turn the maths you see in ML papers into programmer-friendly code patterns.
Table of Contents
Open Table of Contents
Why “Maths-as-Code”?
ML papers speak in symbols. Engineers speak in loops, arrays, and functions. This guide acts as the Rosetta Stone between the two:
- Notation → Meaning in plain English
- Intuition in one line
- Code Equivalent (NumPy/Python first, JS when useful)
Keep this page open when reading a paper; copy the snippets into a notebook and try them on a tiny dataset.
1) Foundations: Scalars, Vectors, Matrices, Tensors
Mental model: A tensor is just an n-dimensional array.
| Math | Meaning | Python / NumPy | Common Use |
|---|---|---|---|
x | Scalar (0-D) | x = 3.14 | Learning rate, bias |
𝐱 = [x₁, …, xₙ] | Vector (1-D) | x = np.array([1,2,3]) | Features, weights |
𝐗 = [[xᵢⱼ]] | Matrix (2-D) | X = np.array([[1,2],[3,4]]) | Weights, batches |
𝓧 | Tensor (n-D) | np.array([...]) | Images, embeddings |
𝐗ᵀ | Transpose | X.T | Switch row/col orientation |
import numpy as np
X = np.array([[1, 2], [3, 4]])
XT = X.T
…and thought “that looks like pseudocode,” this guide is for you.
🧩 2. Scalars, Vectors, Matrices, and Tensors
| Math | Meaning | Python / NumPy | ML Example |
|---|---|---|---|
x | Scalar — single number | x = 3.14 | Learning rate, bias |
𝐱 = [x₁, …, xₙ] | Vector — ordered list | x = np.array([1,2,3]) | Features, weights |
𝐗 = [[xᵢⱼ]] | Matrix — 2D grid | X = np.array([[1,2],[3,4]]) | Weight matrices |
𝓧 | Tensor — n-D array | np.array([...]) / tf.constant([...]) | Images, embeddings |
𝐗ᵀ | Transpose — swap rows/cols | X.T | Switching dimensions |
💡 Intuition:
Scalars → Vectors → Matrices → Tensors
Every ML data structure is one of these.
import numpy as np
X = np.array([[1, 2], [3, 4]])
print("Matrix:", X)
print("Transpose:", X.T)
🔁 3. Summation, Product, and Averages (Σ, ∏, E[·])
Mental model: Σ means loop and add. E[X] means average (expected value).
| Math | Meaning | Code Equivalent | ML Example |
|---|---|---|---|
Σᵢ₌₁ⁿ xᵢ | Sum elements | np.sum(x) | Total loss |
E[X] = (1/n) Σ xᵢ | Expectation / mean | np.mean(x) | Batch average |
∏ᵢ₌₁ⁿ xᵢ | Product | np.prod(x) | Likelihood |
Var(X) | Variance | np.var(x) | Feature scaling |
import numpy as np
x = np.array([1, 2, 3, 4])
np.sum(x), np.mean(x), np.var(x)
⚙️ 4. Linear Algebra Operations
Mental model: Matrices transform vectors. Neural networks are chains of transforms + non-linearities.
| Math | Meaning | Code Equivalent | ML Use | ||||
|---|---|---|---|---|---|---|---|
𝐱·𝐲 = Σ xᵢyᵢ | Dot product | np.dot(x, y) | Similarity, regression | ||||
𝐗𝐘 | Matrix multiply | X @ Y or np.matmul(X,Y) | Forward pass | ||||
| ` | 𝐱 | ₂ = √Σ xᵢ²` | L2 norm | np.linalg.norm(x) | Normalization | ||
𝐈 | Identity matrix | np.eye(n) | Initialization | ||||
𝐗⁻¹ | Inverse | np.linalg.inv(X) | Solving linear systems |
import numpy as np
X = np.array([[1, 2], [3, 4]])
W = np.array([[0.5], [0.2]])
y = X @ W
np.linalg.norm(y)