Skip to content
Go back

Maths-as-Code for Machine Learning

Published:  at  12:00 AM

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:

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.

MathMeaningPython / NumPyCommon Use
xScalar (0-D)x = 3.14Learning 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
𝐗ᵀTransposeX.TSwitch 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

MathMeaningPython / NumPyML Example
xScalar — single numberx = 3.14Learning rate, bias
𝐱 = [x₁, …, xₙ]Vector — ordered listx = np.array([1,2,3])Features, weights
𝐗 = [[xᵢⱼ]]Matrix — 2D gridX = np.array([[1,2],[3,4]])Weight matrices
𝓧Tensor — n-D arraynp.array([...]) / tf.constant([...])Images, embeddings
𝐗ᵀTranspose — swap rows/colsX.TSwitching 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).

MathMeaningCode EquivalentML Example
Σᵢ₌₁ⁿ xᵢSum elementsnp.sum(x)Total loss
E[X] = (1/n) Σ xᵢExpectation / meannp.mean(x)Batch average
∏ᵢ₌₁ⁿ xᵢProductnp.prod(x)Likelihood
Var(X)Variancenp.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.

MathMeaningCode EquivalentML Use
𝐱·𝐲 = Σ xᵢyᵢDot productnp.dot(x, y)Similarity, regression
𝐗𝐘Matrix multiplyX @ Y or np.matmul(X,Y)Forward pass
`𝐱₂ = √Σ xᵢ²`L2 normnp.linalg.norm(x)Normalization
𝐈Identity matrixnp.eye(n)Initialization
𝐗⁻¹Inversenp.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)

Suggest Changes

Previous Post
An Intuitive Journey Through Statistics & Hypothesis Testing
Next Post
Agents, Routing, Patterns, and Actors