Math for ML, by Hand · part 1 of 4

Why Is a Data Point an Arrow?

Math for ML, by Hand (part 1 of 4): vectors, length and angle, worked on one tiny model you can check with a pencil.

Four columns listing twenty topics across four parts; Part 1 is highlighted. Frame 1.
You are in Part 1: vectors, length, angle

A neural network is trained with calculus but described with linear algebra. Most of the confusion people carry about the math of machine learning lives in the seam between those two, and most explanations make it worse by switching examples every few paragraphs.

This series does the opposite. It follows one model so small that every number fits on a napkin, and it asks each branch of math one question about that model. By the end of part 4, every box in the model below has been opened and every number in it has been computed by hand.

The tiny model

Pipeline of five boxes: input x, h equals A x, z equals w dot h plus b, p equals sigma of z, loss equals minus log of the probability given to the true label. Both example users get p = 0.5. Frame 1.
Five boxes: the whole model in this series
Pipeline of five boxes: input x, h equals A x, z equals w dot h plus b, p equals sigma of z, loss equals minus log of the probability given to the true label. Both example users get p = 0.5. Frame 2.
The user who clicked, x = (3, 4), gets p = 0.5
Pipeline of five boxes: input x, h equals A x, z equals w dot h plus b, p equals sigma of z, loss equals minus log of the probability given to the true label. Both example users get p = 0.5. Frame 3.
The user who did not, x′ = (1, 2), also gets p = 0.5. Training has to fix that

Here is the model. Two users visit a site. The first viewed 3 pages and stayed 4 minutes, then clicked. The second viewed 1 page, stayed 2 minutes and left. The model reads each user as two numbers, passes them through a matrix A, a weight vector w and a bias b, and returns a probability of clicking.

Both users come out at p = 0.5. The model cannot tell them apart. Fixing that is what training is for, and it is the thread that runs through all four parts. Part 1 is about the first box: what it means for a user to be the vector x = (3, 4).

Vectors: a data point is an arrow

A data point and an arrow from the origin are the same object, written two ways.

Grid with a dot at (3, 4), then an arrow from the origin to it labelled x, then a second arrow to (1, 2) labelled x prime. Frame 1.
A grid with two axes: pages viewed and minutes
Grid with a dot at (3, 4), then an arrow from the origin to it labelled x, then a second arrow to (1, 2) labelled x prime. Frame 2.
One user: 3 pages, 4 minutes, a dot at (3, 4)
Grid with a dot at (3, 4), then an arrow from the origin to it labelled x, then a second arrow to (1, 2) labelled x prime. Frame 3.
The arrow from the origin to the dot is the vector x
Grid with a dot at (3, 4), then an arrow from the origin to it labelled x, then a second arrow to (1, 2) labelled x prime. Frame 4.
A second user x′ = (1, 2): another arrow

Draw two axes: pages viewed across, minutes on site up. Walk 3 along and 4 up and put a dot there. That dot is the first user.

Now draw an arrow from the origin to the dot. The arrow is the vector x. It carries exactly the information the two numbers carry, nothing more. What the picture adds is a set of moves you can see: arrows can be laid end to end, stretched, compared by length and compared by direction.

The second user, x′ = (1, 2), is a shorter arrow pointing in nearly the same direction. Keep that in mind. Most of this part is about measuring how far apart, and how aligned, these two arrows are.

By hand
x  = [3, 4]     ← pages viewed, minutes on site
x′ = [1, 2]
shape: (2,)  one row of the data table = one arrow in R²

Why ML cares. Every input a model sees is a vector: a row of features, a pixel grid flattened into one long list, a 768-number text embedding. The picture of an arrow still holds in 768 dimensions; we just cannot draw it.

What it cannot do. The picture says where x is. It does not yet say how to combine arrows.

Adding and scaling

Add arrows tip to tail; scale them by stretching.

Tip-to-tail addition of (3, 4) and (1, 2) gives (4, 6); doubling (3, 4) gives (6, 8). Frame 1.
x = (3, 4) and x′ = (1, 2)
Tip-to-tail addition of (3, 4) and (1, 2) gives (4, 6); doubling (3, 4) gives (6, 8). Frame 2.
Slide x′ so its tail sits on the tip of x
Tip-to-tail addition of (3, 4) and (1, 2) gives (4, 6); doubling (3, 4) gives (6, 8). Frame 3.
The new tip is x + x′ = (4, 6): add position by position
Tip-to-tail addition of (3, 4) and (1, 2) gives (4, 6); doubling (3, 4) gives (6, 8). Frame 4.
2x = (6, 8): same direction, twice as long

To add x′ to x, slide x′ so its tail sits on the tip of x. The new tip lands at (4, 6). In numbers this is just position-by-position addition: 3 + 1 and 4 + 2.

Scaling is simpler. 2x = (6, 8) doubles both entries, and the arrow gets twice as long without turning. Multiply by −1 and the arrow flips to point the other way.

A vector space is any collection of objects where these two moves behave the way they do on this grid. The formal definition is a list of rules (addition is commutative, there is a zero vector, scaling distributes over addition, and so on). For machine learning, the practical reading is: anything you can add and scale like arrows can be fed to the rest of this series.

By hand
x + x′ = [3+1, 4+2] = [4, 6]
2·x    = [2·3, 2·4] = [6, 8]
−1·x   = [−3, −4]          same length, opposite direction

Why ML cares. A weighted sum of vectors, a·x + b·x′, is the most common operation in a network. Every neuron computes one.

What it cannot do. Nothing here says how long an arrow is. For that we need a norm.

Length in two dimensions

The length of an arrow is the Pythagorean theorem.

The arrow (3, 4) as the long side of a right triangle with legs 3 and 4; squares of area 9 and 16 add to 25; the length is 5. Frame 1.
How long is x?
The arrow (3, 4) as the long side of a right triangle with legs 3 and 4; squares of area 9 and 16 add to 25; the length is 5. Frame 2.
Tempting: add the coordinates, 3 + 4 = 7
The arrow (3, 4) as the long side of a right triangle with legs 3 and 4; squares of area 9 and 16 add to 25; the length is 5. Frame 3.
x is the long side of a right triangle with legs 3 and 4
The arrow (3, 4) as the long side of a right triangle with legs 3 and 4; squares of area 9 and 16 add to 25; the length is 5. Frame 4.
Squares on the legs: 9 + 16 = 25
The arrow (3, 4) as the long side of a right triangle with legs 3 and 4; squares of area 9 and 16 add to 25; the length is 5. Frame 5.
Length ‖x‖ = √25 = 5

The tempting answer to "how long is x?" is 3 + 4 = 7. That number is real, but it counts grid steps, not the length of the arrow.

The arrow is the long side of a right triangle with legs 3 and 4. Put a square on each leg: their areas are 9 and 16, and they add to 25. The square on the long side has the same area, so the arrow has length √25 = 5.

The notation ‖x‖₂ with its square root looks heavier than the idea. It is Pythagoras with a subscript.

By hand
x          = [3, 4]
squares    = [9, 16]
sum        = 25
‖x‖₂ = √25 = 5

Why ML cares. Lengths turn up as weight decay (the L2 penalty adds ‖w‖² to the loss), as gradient clipping (rescale when ‖∇L‖ is too large) and as normalisation layers.

What it cannot do. Two coordinates only. Real inputs have more.

Length in three dimensions (and more)

Each extra coordinate adds one more right triangle.

A 3-D arrow to (3, 4, 12): the floor diagonal has length 5, the vertical leg 12, so the arrow has length 13. Frame 1.
Add a third feature: scroll depth. x = (3, 4, 12)
A 3-D arrow to (3, 4, 12): the floor diagonal has length 5, the vertical leg 12, so the arrow has length 13. Frame 2.
Along the floor: the 2-D length is 5
A 3-D arrow to (3, 4, 12): the floor diagonal has length 5, the vertical leg 12, so the arrow has length 13. Frame 3.
Straight up: the third coordinate is 12
A 3-D arrow to (3, 4, 12): the floor diagonal has length 5, the vertical leg 12, so the arrow has length 13. Frame 4.
Pythagoras again: √(5² + 12²) = 13

Give the first user a third feature, scroll depth 12, so x = (3, 4, 12). Along the floor the arrow covers the 2-D distance we already know, 5. Then it climbs 12 straight up.

Those two pieces form a new right triangle with legs 5 and 12, and Pythagoras gives √(25 + 144) = √169 = 13. Expanding the 5 back into 3 and 4 gives the general rule: square every coordinate, add, take the root.

In n dimensions the triangle step repeats n − 1 times, and the formula keeps the same shape: ‖x‖₂ = √(x₁² + … + xₙ²).

By hand
x          = [3, 4, 12]
squares    = [9, 16, 144]
sum        = 169
‖x‖₂ = √169 = 13        (= √(5² + 12²), Pythagoras twice)

Why ML cares. A text embedding with 768 coordinates has a length computed exactly this way. Cosine similarity, used to compare embeddings, divides by two of these lengths.

What it cannot do. One way of measuring length is not the only way, and it is not always the right one.

Distance: L2, L1 and the rest of the family

A distance is the norm of the difference, and there is more than one legitimate norm.

Two points (1, 2) and (3, 4): the straight-line distance is 2.83 and the grid distance is 4. Then the unit balls for L1, L2, L-infinity and p = 0.5. Frame 1.
How far apart are x = (3, 4) and x′ = (1, 2)?
Two points (1, 2) and (3, 4): the straight-line distance is 2.83 and the grid distance is 4. Then the unit balls for L1, L2, L-infinity and p = 0.5. Frame 2.
Straight line (L2): 2.83
Two points (1, 2) and (3, 4): the straight-line distance is 2.83 and the grid distance is 4. Then the unit balls for L1, L2, L-infinity and p = 0.5. Frame 3.
Along the grid (L1): 2 + 2 = 4
Two points (1, 2) and (3, 4): the straight-line distance is 2.83 and the grid distance is 4. Then the unit balls for L1, L2, L-infinity and p = 0.5. Frame 4.
Tempting: the staircase is longer, so it must be wrong
Two points (1, 2) and (3, 4): the straight-line distance is 2.83 and the grid distance is 4. Then the unit balls for L1, L2, L-infinity and p = 0.5. Frame 5.
Both are distances. Each norm has its own 'circle' of points at distance 1

The gap between the two users is x − x′ = (2, 2). Measured in a straight line (p = 2) that is 2.83. Measured along the grid (p = 1, the Manhattan or taxicab norm) it is 2 + 2 = 4. With p = ∞ you keep only the largest coordinate, 2.

It is tempting to call the longer staircase "wrong". It is not. A taxi on a street grid really does drive 4 blocks. Each norm answers a different question, and each has its own shape of "all points at distance 1": a diamond for L1, a circle for L2, a square for L∞.

Any function that behaves like length earns the name norm: it is zero only for the zero vector, it scales with the vector, and it obeys the triangle inequality, ‖a + b‖ ≤ ‖a‖ + ‖b‖. That last rule is why p must be at least 1. At p = 0.5, ‖e₁ + e₂‖ = 4 while ‖e₁‖ + ‖e₂‖ = 2. The shortcut is longer than the detour, so the formula has stopped measuring a distance.

By hand
x − x′ = [2, 2]
L2: √(2² + 2²) = 2.83
L1: |2| + |2|   = 4
L∞: max(2, 2)   = 2
p = 0.5:  ‖(1,1)‖ = (1 + 1)² = 4  >  ‖(1,0)‖ + ‖(0,1)‖ = 2   ✗ not a norm

Why ML cares. L1 penalties push weights to exactly zero (sparse models); L2 penalties shrink them smoothly. Nearest-neighbour search and k-means change their answers when you change the norm.

What it cannot do. A norm measures size, but not direction. Two arrows can have equal lengths and point in opposite directions.

Inner products: one number for agreement

Multiply matching coordinates and add: the result says how much two arrows agree.

The inner product of x with three vectors: 11 with x prime, 0 with (−4, 3), and −3 with (−1, 0). Frame 1.
Multiply position by position and add: 3·1 + 4·2 = 11
The inner product of x with three vectors: 11 with x prime, 0 with (−4, 3), and −3 with (−1, 0). Frame 2.
Large and positive: x and x′ point almost the same way (cos θ = 0.984)
The inner product of x with three vectors: 11 with x prime, 0 with (−4, 3), and −3 with (−1, 0). Frame 3.
x and (−4, 3): 3·(−4) + 4·3 = 0, the arrows are at a right angle
The inner product of x with three vectors: 11 with x prime, 0 with (−4, 3), and −3 with (−1, 0). Frame 4.
Point against x, like (−1, 0): the inner product is −3

Multiply the two users position by position and add: 3·1 + 4·2 = 11. The result is large and positive because the arrows point almost the same way. Divide by the two lengths and you get cos θ = 0.984, an angle of 10.3°.

Turn x by a right angle to (−4, 3) and the same recipe gives 3·(−4) + 4·3 = 0. Zero is the signature of a right angle; such vectors are called orthogonal. Point against x, for instance with (−1, 0), and the result goes negative: −3.

So unlike a norm, an inner product can be negative. It maps two vectors to any real number. What it cannot be is negative for a vector paired with itself, because ⟨x, x⟩ = ‖x‖² = 25. That link runs both ways: every inner product defines a norm by ‖x‖ = √⟨x, x⟩.

By hand
x       = [3,  4]
x′      = [1,  2]
x ⊙ x′  = [3,  8]    → sum 11
cos θ   = 11 / (5 · √5) = 0.984
x · (−4, 3) = −12 + 12 = 0     orthogonal

Why ML cares. The score in our model, z = w·h + b, is an inner product: it measures how much the hidden vector h agrees with the learned direction w. Attention scores in a transformer are inner products of query and key vectors.

What it cannot do. Agreement between two arrows is still relative to the axes we drew. Change the axes and the coordinates change, which needs the idea of a basis.

Bases: coordinates are measurements

A basis is a set of measuring sticks; with an orthonormal basis, each coordinate is one inner product.

x = (3, 4) in the standard basis and in a basis tilted by 45 degrees, where its coordinates are 4.95 and −0.71. Frame 1.
The standard basis: e₁ = (1, 0), e₂ = (0, 1)
x = (3, 4) in the standard basis and in a basis tilted by 45 degrees, where its coordinates are 4.95 and −0.71. Frame 2.
x = 3e₁ + 4e₂: the coordinates are the weights
x = (3, 4) in the standard basis and in a basis tilted by 45 degrees, where its coordinates are 4.95 and −0.71. Frame 3.
A tilted orthonormal basis q₁, q₂ (the same lengths, still at 90°)
x = (3, 4) in the standard basis and in a basis tilted by 45 degrees, where its coordinates are 4.95 and −0.71. Frame 4.
New coordinates are inner products: ⟨x, q₁⟩ = 4.95, ⟨x, q₂⟩ = −0.71
x = (3, 4) in the standard basis and in a basis tilted by 45 degrees, where its coordinates are 4.95 and −0.71. Frame 5.
Rebuild: 4.95·q₁ + (−0.71)·q₂ lands back on x

The numbers 3 and 4 are weights on two fixed arrows, e₁ = (1, 0) and e₂ = (0, 1): x = 3e₁ + 4e₂. Any set of arrows that can build every vector in this way, with none of them redundant, is a basis.

Tilt the measuring sticks by 45°: q₁ = (1, 1)/√2 and q₂ = (1, −1)/√2. They still have length 1 and still meet at a right angle, so the basis is orthonormal. In this basis the coordinates of x are found by one inner product each: ⟨x, q₁⟩ = 7/√2 = 4.95 and ⟨x, q₂⟩ = −1/√2 = −0.71. Rebuild 4.95·q₁ + (−0.71)·q₂ and you land exactly back on (3, 4).

That shortcut only works because the sticks are orthonormal. For a skewed basis you would have to solve a small system of equations instead. The tilted sticks are not arbitrary, either: (1, 1) and (1, −1) are the directions that our matrix A only stretches, which is where part 2 picks up.

By hand
q₁ = [1, 1]/√2     q₂ = [1, −1]/√2
⟨x, q₁⟩ = (3 + 4)/√2 = 4.95
⟨x, q₂⟩ = (3 − 4)/√2 = −0.71
4.95·q₁ + (−0.71)·q₂ = [3, 4]   ✓
Gram grid ⟨qᵢ, qⱼ⟩ = [[1, 0], [0, 1]]

Why ML cares. PCA chooses an orthonormal basis aligned with the directions where the data varies most, then keeps only the first few coordinates. Every coordinate it keeps is one inner product.

What it cannot do. Everything so far is static: arrows sitting still. A model has to move them, and moving every arrow at once is what a matrix does.

Where this leaves us

The habit to carry forward is the one the inner product teaches: a single number that says how much two vectors agree. The score z = w·h + b in our model is exactly that, an inner product plus a constant.

Part 2 asks what the matrix A does to x before that score is taken. It moves the whole grid at once, and the way it moves the grid explains matrix products, determinants, eigenvectors and the SVD.

Next: part 2, What Does a Weight Matrix Actually Do to Your Data?