Geometry in AI comes down to two questions: where is a point, and how far apart are two points? Coordinates answer the first with numbers, and Pythagoras answers the second. The same distance formula works with three coordinates, or three hundred, which is how a model decides that two pictures, two words or two customers are similar: their lists of numbers are close. This lesson builds that distance and uses it to make a first prediction. Along the way Python gets the tuple, a small fixed group of values that suits a point exactly, and you'll meet the catch that every distance-based model has to handle: the units of each number decide what "close" means.
What you'll be able to do
Place points with coordinates, (x, y), over then up, in all four quadrants
Find the distance between two points with Pythagoras, in two coordinates or more
Store points as tuples in Python, write a distance function for any number of coordinates, and graph a distance
Predict from the nearest neighbor: in ML, "similar" means "close"
Quick check
Have a guess before you start. You walk 3 blocks east, then 4 blocks north. How far are you from where you started, in a straight line?
5 blocks
12 blocks
7 blocks
1 block
Show the answer
5 blocks
The walk makes a right triangle with sides 3 and 4. The straight line is the long side: √(3² + 4²) = √25 = 5.
The math: where, and how far
Purpose. Turn "where" and "how far" into numbers.
A point on a flat grid is written (x, y): go over x, then up y, from the origin (0, 0). Negative x is left and negative y is down, so the two axes cut the plane into four quadrants. (3, 4) is up and right; (−2, 1) is up and left.
Pythagoras. In a right triangle with short sides a and b, the long side c satisfies
a2+b2=c2
In plain words: square the two short sides and add them; the square root of that is the long side.
The 3-4-5 triangle is the classic: 9 + 16 = 25, and √25 = 5.
Quick check
From an earlier lesson: what is √(6² + 8²)?
14
48
100
10
Show the answer
10
6² + 8² = 36 + 64 = 100, and √100 = 10. Roots undo squares, as lesson 10 showed.
The distance between two points. Any two points sit at the corners of a right triangle: the difference in x is one short side, the difference in y the other. So their distance is Pythagoras on the differences. Watch it for (1, 2) and (4, 6). The picture draws each step on a grid with the same scale across and up: the difference across and the difference up become the two short sides of a right triangle, and last comes the straight line between the points, its long side.
Try it now
Then set the first point to 0, 0, 0 and the second to 1, 2, 2 and press Use these numbers: three coordinates, the same steps. A flat picture has no room for a third direction, so that one runs without it.
Symbols and formula. For points (x₁, y₁) and (x₂, y₂):
d=(x2−x1)2+(y2−y1)2
In plain words: the difference across, squared, plus the difference up, squared; then the square root.
More coordinates, same idea. A point in space has three coordinates, (x, y, z), and the distance just gets a third square under the root. With any number of coordinates, the rule is the same: square each difference, add them all, take the root. That's called the Euclidean distance.
Quick check
What's the distance from (−2, 1) to (1, −3)?
1
25
5
7
Show the answer
5
Differences: 1 − (−2) = 3 and −3 − 1 = −4. Squared: 9 and 16, adding to 25. √25 = 5.
Quick check
Which quadrant is (−3, −5) in?
Bottom right: x positive, y negative
Top right: both positive
Top left: x negative, y positive
Bottom left
Show the answer
Bottom left
Negative x is left of the origin and negative y is below it: the bottom-left quadrant.
Three facts that always hold. A distance is never negative, because it's a square root of squares. It's 0 only when the two points are the same point. And it doesn't matter which point you start from: (1, 2) to (4, 6) is 5, and (4, 6) to (1, 2) is 5 too, because each difference only flips its sign and the squares don't change.
Check with a walk. The straight-line distance is never longer than going across and then up: from (1, 2) to (4, 6) the walk along the grid is 3 + 4 = 7, and the straight line is 5. If your answer is longer than the walk, something went wrong.
In your own words
In your own words: why does squaring the differences mean it doesn't matter which point you start from?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: points as tuples
Purpose. Store a point as one value, and measure between any two.
A tuple is a fixed list of values in round brackets. It's the natural way to hold a point:
p = (3, 4)
print(p[0], p[1], len(p))
x, y = p
print(x, y)
It runs right here in your browser, with nothing to install.
p[0] and p[1] read the coordinates, like a list. The line x, y = punpacks the tuple, giving each coordinate its own name. Unlike a list, a tuple can't be changed after it's made, which suits a point that should stay put. A distance function takes two of them:
Any number of coordinates. A loop over the coordinates handles points of any size, two or two hundred:
import math
defdist(p, q):
total = 0for i inrange(len(p)):
total = total + (q[i] - p[i]) ** 2return math.sqrt(total)
print(dist((0, 0, 0), (1, 2, 2)), dist((1, 2, 3, 4), (2, 3, 4, 5)))
Now step through the same function, called twice: once on the flow's two points, once on two points in space, with three coordinates each. Press Next and watch total grow by one squared difference on each pass of the loop; the return then hands back its square root, the distance. The points p and q show as rows of boxes, like the cells in the flow above.
A named mistake: *2 for **2. Writing (q[i] - p[i]) * 2 doubles a difference instead of squaring it. It runs without an error and gives a wrong distance. The 3-4-5 check catches it: dist((0, 0), (3, 4)) must be 5.
Worked example
Worked example: the nearest of five points
Find which of five points is nearest to the target (3, 3).
import math
defdist(p, q):
total = 0for i inrange(len(p)):
total = total + (q[i] - p[i]) ** 2return math.sqrt(total)
pts = [(1, 7), (4, 4), (6, 1), (2, 1), (5, 6)]
target = (3, 3)
best = 0for i inrange(len(pts)):
if dist(pts[i], target) < dist(pts[best], target):
best = i
print(best, pts[best], round(dist(pts[best], target), 3))
In the lesson you can change this and run it.
A list of tuples holds the five points. The search is lesson 19's hand-written search: keep the position of the best so far, and update it whenever a point is closer. Point 1, (4, 4), wins at √2 ≈ 1.414 away. (2, 1) looks close too, but it's √5 ≈ 2.236 away, because it's off by 2 in y.
Quick check
What does this print?
p = (2, 7)
x, y = p
print(y - x, p[1])
7 5
-5 7
5 2
5 7
Show the answer
5 7
Unpacking gives x = 2 and y = 7, so y − x = 5. p[1] is the second value, 7.
Quick check
Which line squares the difference in coordinate i?
(q[i] - p[i]) ** 2
(q[i] - p[i]) * 2
(q[i] - p[i]) ^ 2
q[i] - p[i] ** 2
Show the answer
(q[i] - p[i]) ** 2
** 2 squares the whole difference, which the brackets keep together.
A distance as a graph. The worked example measured five points, one at a time. A graph shows a whole row of distances at once. The trail club's path runs along the x-axis, and one of its signs stands off the path at (3, 4). Each spot on the path is a point (x, 0), so its differences from the sign are x − 3 and 0 − 4. The loop below works out the distance from seven spots, and Matplotlib, as in lesson 19, draws them:
import math
import matplotlib.pyplot as plt
xs = [0, 1, 2, 3, 4, 5, 6]
ds = []
for x in xs:
ds.append(math.sqrt((x - 3) ** 2 + (0 - 4) ** 2))
plt.plot(xs, ds)
plt.xlabel("x, a spot on the path")
plt.ylabel("distance to the sign")
plt.show()
The curve falls as you walk toward the sign and rises again once you've passed it.
Quick check
Read the graph. Where on the path are you nearest the sign, and how far away is it there?
At x = 3, 4 away
At x = 0, 5 away
At x = 3, 0 away
At x = 4, 3 away
Show the answer
At x = 3, 4 away
The curve is lowest at x = 3, straight under the sign, where only the 4 up is left: √(0² + 4²) = 4. It can't reach 0, because the sign is off the path. At x = 0 and x = 6 it's √(3² + 4²) = 5, the 3-4-5 triangle again.
Drill
A distance for any number of coordinates
Fix dist(p, q) so it gives the Euclidean distance for points of any length, then set d2 and d3 to two distances.
import math
defdist(p, q):
total = 0for i inrange(len(p)):
total = total + (q[i] - p[i]) * 2return total
d2 = dist((0, 0), (3, 4))
d3 = dist((1, 1, 1), (3, 4, 7))
print(d2, d3)
In the lesson, your code is checked as you write it.
In your own words
Why is a tuple a good fit for a point?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: similar means close
Purpose. Many models judge similarity by distance: two things are alike if their lists of numbers are close.
Describe each day at the tent shop by two numbers, (temperature, humidity). On four days the shop rented 22, 41, 59 and 47 tents. To predict a new day, find the nearest neighbor, the old day closest to it, and predict that day's rentals. This is the k-nearest-neighbors idea with k = 1.
y^=yj,where day j minimizes d(xj,xnew)
In plain words: the prediction is the answer of whichever known example is nearest.
import math
defdist(p, q):
total = 0for i inrange(len(p)):
total = total + (q[i] - p[i]) ** 2return math.sqrt(total)
days = [(10, 80), (20, 50), (30, 30), (25, 60)]
new = (22, 55)
for i inrange(len(days)):
print(days[i], round(dist(days[i], new), 2))
Quick check
From the distances above, which day is the new day's nearest neighbor, and what does it predict?
(25, 60), predicting 47 tents
(20, 50), predicting 41 tents
(30, 30), predicting 59 tents
(10, 80), predicting 22 tents
Show the answer
(20, 50), predicting 41 tents
(20, 50) is 5.39 away, the smallest. Its 41 rentals become the prediction.
The picture. Here are the four old days and the new one on a grid, temperature across and humidity up, with a dashed line from the new day to each. Two days are far off; two sit close. To fit all five, this first view squeezes the humidity: a step up is drawn shorter than a step across, so read the far days by their numbers. Press Next to zoom in on the two close days, drawn to the same scale across and up, so each line's length is its true distance.
In the real tools. In PyTorch, the library most models are built with, the four days go in one table, a tensor, and one line measures the new day's distance to every row at once, with no loop:
A catch: units matter. Distance treats every coordinate the same. If one were in dollars and another in cents, the cents would swamp the distance, and the nearest neighbor would be chosen by the cents alone. That's why ML code usually scales each coordinate first, as in lesson 20, so a step of 1 means about the same in each. Distance is simple, but which numbers go into it decides what "similar" means.
Here the same two days are described by temperature and by the day's takings in cents. A 10-degree difference in weather barely registers next to a 100-cent difference in money.
Quick check
What does this print?
import math
a = (20, 5000)
b = (30, 5100)
print(round(math.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2), 1))
110.0
10.0
100.5
14.1
Show the answer
100.5
The differences are 10 and 100, so the distance is √(100 + 10000) ≈ 100.5. Almost all of it comes from the cents column.
Drill
Predict from the nearest day
Fix the search so best is the position of the day nearest to new, and prediction is that day's rentals.
import math
defdist(p, q):
total = 0for i inrange(len(p)):
total = total + (q[i] - p[i]) ** 2return math.sqrt(total)
days = [(10, 80), (20, 50), (30, 30), (25, 60)]
rentals = [22, 41, 59, 47]
new = (28, 36)
best = 0for i inrange(len(days)):
if dist(days[i], new) > dist(days[best], new):
best = i
prediction = rentals[0]
print(best, prediction)
In the lesson, your code is checked as you write it.
In your own words
How does a nearest-neighbor model decide what to predict, and what does it need distance for?
Write a sentence or two, then compare it with a good answer in the lesson.
31Formulas with fractions: clear them firstFreeClearing denominators, cross-multiplying, a letter in the bottom, and reciprocal formulas like 1/R = 1/R₁ + 1/R₂.35 min
37Inverse functions, lines and scalingFreeUndoing a function by making x the subject, domains that allow it, line forms, and unscaling predictions.40 min
38Sequences and sums: find n, a, d and rFreeEvery letter of the arithmetic and geometric formulas as the subject, and an average's window from its decay.40 min
40Review: rearrange anything (algebra)FreeBroken rearrangements to fix, mixed practice from the whole algebra block, a strategy chart, and its formula sheet.35 min
41Coordinates and distanceFreePoints as (x, y), Pythagoras, distance in any number of coordinates, and nearest neighbors.42 min
42Angles, degrees and radiansFreeMeasuring a turn, converting units, arcs, and the angle between two arrows.36 min
43Sine and cosineFreeAcross and up: right triangles, the unit circle, and directions and rotations.42 min
44Waves: height, speed and shiftFreeAmplitude, period and phase, sampling a wave, and position codes in transformers.37 min
68Vector equations: rearranging with arrowsFreeSolving for a vector or a scale, recovering a gradient from two weight snapshots, and an angle from a dot product.40 min
87Counting: choices, orders and groupsFreeThe counting principle, factorials, ordered picks and groups, and why attention's cost grows with the square of the length.43 min
88Probability: from counts to chancesFreeEqually likely outcomes, the complement, distributions, seeded simulation, and a next-word model.43 min
89Combining events: and, or, independentFreeThe or rule, independence, probability trees, drawing with and without replacement, and chains of steps.42 min
100Maximum likelihoodFreeLikelihoods as products, log-likelihoods as sums, grid search, underflow, and the negative log-likelihood loss.45 min
101Linear regression: the best lineFreeResiduals, least squares by formula, three roads to one line, the matrix form, and train versus test error.45 min
109One neuron, start to finishFreeWeighted sum, activation, loss, slopes and update, by hand and in code, and what one straight line can and can't split.45 min
111Training a small networkFreeThe backward pass in matrix form, gradient checks, a full training loop, and reading loss curves.45 min
112The same network in PyTorchFreenn.Sequential with the NumPy weights loaded, the same loss and slopes, then SGD and mini-batches.45 min
113Attention as matrix mathFreeScaled dot-product scores, the causal mask, a row-by-row softmax, and blending value vectors.45 min
114Residual connections and layer normFreeAdding the input back and why its slope keeps learning alive, layer norm by hand, and add and norm.40 min
115A transformer block, start to finishFreeA sentence through embeddings, attention, add and norm, a small network and next-word scores, every shape named.45 min
117Final test: transpose any formulaFreeSlips to fix from every topic, mixed practice of every kind of rearranging, the strategy chart and the full sheet.45 min
Learn the math behind AI from arithmetic up: numbers, algebra, vectors and matrices, calculus, probability and statistics, each shown as numbers moving and checked in Python, ending with a network you build and train from the formulas.
See the course →
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write