Learn machine learning from scratch

Start at chapter 1 and build up: what a model is, how it measures its own mistakes, and how it learns. Everything is worked out in plain Python, with the maths shown rather than assumed. Two shorter tracks hold reference notes on the maths and on modern architectures.

Track Progress: 0 Completed
0%
Maths for MLLEVEL 1 · BEGINNERModule 0 · Reading maths

Letters and the little numbers: xᵢ, x², x⁽ⁱ⁾

Subscripts pick an item, superscripts are usually powers, and a superscript in brackets means a training example. Three tiny marks, three different meanings.

9 min Prerequisites: None: this is the first lesson

1. The idea

Maths notation is compressed code. One line in a paper can stand for a loop over a dataset, a loop over features and a sum, all at once. The first skill is reading the small numbers and letters stuck to a symbol, because they say which item you are looking at.

A subscript picks an item. xᵢ is item number i of the list x, and Xᵢⱼ is the entry in row i, column j of the table X. A superscript is usually a power: x² is x times x. Machine learning adds a third kind, a superscript in brackets, x⁽ⁱ⁾, which means “training example number i” and has nothing to do with powers. So x₂ is the second feature, x² is x squared, and x⁽²⁾ is the second example in the dataset.

One trap catches everyone once: maths counts from 1, Python counts from 0. The x₁ of a paper is x[0] in NumPy. When you turn an equation into code, shift every index down by one.

2. Try it: point at an entry

Here is a small dataset: 4 examples (rows) and 3 features (columns). Pick i and j and see which entry xⱼ⁽ⁱ⁾ names, and how you'd write it in NumPy.

A table of 4 rows (examples) by 3 columns (features) of numbers. Sliders for i and j highlight row i and column j; the entry where they cross is x sub j superscript (i), which NumPy writes as X[i - 1, j - 1].

3. The math

Subscript: which item
x=(x1,x2,…,xd)Xij=row i, column j\mathbf{x} = (x_1, x_2, \dots, x_d) \qquad X_{ij} = \text{row } i,\ \text{column } j
A subscript is an index into a list or table. Two subscripts mean row, then column.
Superscript: a power
x2=x⋅xx3=x⋅x⋅xx^2 = x \cdot x \qquad x^3 = x \cdot x \cdot x
With no brackets, a raised number multiplies x by itself.
Superscript in brackets: which example
x(i)=(x1(i),x2(i),…,xd(i))\mathbf{x}^{(i)} = \left(x^{(i)}_1, x^{(i)}_2, \dots, x^{(i)}_d\right)
The brackets say “this is a label, not a power”. x⁽ⁱ⁾ is the i-th row of the data, a whole example.
Both at once
xj(i) = Xijx^{(i)}_{j} \,=\, X_{ij}
Feature j of example i: the same number, written two ways.

4. The code (python)

mm0_indices.py
import numpy as np

# 4 examples (rows) and 3 features (columns)
X = np.array([[1.2, 3.0, 0.5],
              [0.8, 2.0, 1.5],
              [2.5, 4.0, 0.2],
              [1.9, 1.0, 3.3]])

i, j = 2, 3                   # the maths counts from 1
print(X[i - 1, j - 1])        # 1.5
print(X[i - 1])               # [0.8 2.  1.5]
print(X[:, j - 1] ** 2)       # [ 0.25  2.25  0.04 10.89]

5. You’ll see this in

6. Check yourself

Answer all 4 questions correctly to complete the chapter · 0 / 4 done
Q1/4 In a machine learning paper, what does x⁽³⁾ most likely mean?
Q2/4 A paper's w₁ is which element of the NumPy array w?
Q3/4 X has one row per example and one column per feature. What is X₄₂?
Q4/4 Which of these is a power rather than an index?

Finished this chapter?

Weekly newsletter

What changed in ML architectures this week

New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.