Math for AI course · Lesson 43 of 180
Distance depends on the chosen geometry
Derive distance from a norm, compare a tiny nearest-neighbor search under L1, L2, and L-infinity, and debug metric choices in AI systems.
When a support-ticket router, recommender, or retrieval system looks for the “closest” stored example, it is making a geometric choice. A point that is closest when every coordinate contributes separately can lose to a different point when the coordinates combine by squared length. The algorithm did not become inconsistent; the meaning of “close” changed.
By the end of this lesson, you will be able to derive a distance from a norm, calculate a small nearest-neighbor ranking under several metrics, and identify the preprocessing and tie-handling decisions that make that ranking meaningful.
Learning outcome: Derive distance from a norm and compare nearest neighbors.
This lesson builds on Lesson 040: L1, L2, and L-infinity See Size Differently, which introduced several ways to measure the size of one vector. Section 3.3 of the official Mathematics for Machine Learning book supplies the mathematical reference. The dataset, calculations, code, visual, and assessments here are original.
The closest point starts with a displacement
Let and be two points in . Each coordinate is a feature position; is the number of features. To ask how far is from , first subtract coordinate by coordinate:
This difference is a displacement vector. It says how much one point must move in each coordinate to land on the other. The order changes the sign of the displacement, but a norm gives the same size either way because .
The names describe different movement rules. L1, often called Manhattan distance, adds the amount moved in every coordinate. L2 measures the straight line length of the displacement. L-infinity only asks for the largest single coordinate movement. A model that uses one of these measures is declaring which kinds of movement count as expensive.
The Section 3.3 reference also connects an inner product to a norm through . With the usual dot product, this is the Euclidean norm, so . A useful subtlety is that the distance construction works for any norm: L1 is a valid norm and induces a valid metric even though it is not induced by an inner product in the usual real-vector-space sense.
Why subtraction must happen first
The distance compares two locations through one displacement. It is not the difference between their separate sizes. For example, if and , both points have L2 norm , but
Computing would erase the fact that the points sit two units apart vertically. The safe mental recipe is subtract, then apply the chosen norm.
The same points can have different nearest neighbors
Consider a query point and three candidate records in a two-feature representation. The coordinates are deliberately small and unitless: they stand for values after some chosen feature representation, not for a universal notion of similarity.
| Point | Description | Coordinates |
|---|---|---|
| axis-heavy case | ||
| balanced case | ||
| mixed case |
The ranking changes because the norms reward different shapes of displacement. L1 favors the axis-heavy point here because it has the smallest total coordinate movement. L2 picks : its two coordinates combine to a shorter straight-line segment than ‘s diagonal and ‘s long horizontal segment. L-infinity picks because no individual coordinate moves by more than .
One query, three neighbor maps
| Candidate | Coordinates | L1sum of absolute moves | L2straight-line length | L-infinitylargest coordinate move |
|---|---|---|---|---|
| A axis-heavy | (6, 0) | 6.000rank 1 | 6.000rank 3 | 6.000rank 3 |
| B balanced | (4, 4) | 8.000rank 3 | 5.657rank 2 | 4.000rank 1 |
| C mixed | (5, 2) | 7.000rank 2 | 5.385rank 1 | 5.000rank 2 |
| Two nearest candidates | A → C | C → B | B → C | |
Numbers inside the circles give the rank for that metric. The dashed segment points to the nearest candidate; the first two ranks are outlined so the map does not rely on color alone.
For L1, the order is A then C then B. For L2, it is C then B then A. For L-infinity, it is B then C then A. The query is at the origin in every panel.
The table and rank labels make the conclusion available without reading the SVG. The visual adds one spatial cue: the dashed segment changes endpoint when the metric changes, while the coordinates themselves remain fixed.
A transparent NumPy search
The NumPy numpy.linalg.norm documentation
exposes the two controls needed here: ord chooses the norm order and axis
chooses which coordinate axis to reduce. A batch of three candidate rows is
therefore handled by subtracting the query from every row and reducing each row
separately.
The following code mirrors the derivation rather than hiding it in a nearest neighbor object. It was executed with NumPy; the printed ordering is the same as the worked example.
import numpy as np
query = np.array([0.0, 0.0])
points = np.array([[6.0, 0.0], [4.0, 4.0], [5.0, 2.0]])
labels = np.array(["A", "B", "C"])
difference = points - query
for name, order in [("L1", 1), ("L2", 2), ("L-infinity", np.inf)]:
distances = np.linalg.norm(difference, ord=order, axis=1)
ranked = np.argsort(distances, kind="stable")
print(
f"{name}: "
+ ", ".join(f"{labels[i]}={distances[i]:.3f}" for i in ranked)
)
L1: A=6.000, C=7.000, B=8.000
L2: C=5.385, B=5.657, A=6.000
L-infinity: B=4.000, C=5.000, A=6.000
There are three separate operations worth noticing:
points - queryperforms the subtraction before any norm is applied.axis=1computes one norm per row, so each candidate gets one distance.argsortturns distances into a ranking; the norm itself does not decide how to handle equal values.
For a larger data set, scikit-learn’s NearestNeighbors
class packages the same idea. Its metric="minkowski" setting uses the
parameter p, with corresponding to L1 and to L2; its
kneighbors method returns both distances and indices. The estimator is useful
when the search should be integrated into a model pipeline, but the NumPy
version is the better debugging tool because every intermediate array is visible.
For this exact example, the equivalent search configuration is:
from sklearn.neighbors import NearestNeighbors
search = NearestNeighbors(n_neighbors=2, metric="minkowski", p=2)
search.fit(points)
distances, indices = search.kneighbors(query.reshape(1, -1))
nearest_labels = labels[indices[0]]
Changing p to 1 or np.inf changes the geometry while keeping the data
and the value of fixed. The API does not make the metric choice
disappear; it makes that choice explicit in a parameter.
Boundaries that change the answer
Distance is precise only after the feature space, metric, and search policy are specified. These are the cases most likely to make a small nearest-neighbor demo misleading.
Ties are information, not noise
Let , , and . Both candidates have
If , the mathematics supplies a tie, not a unique winner. A production
system should state a deterministic secondary key, return all tied candidates,
or use a policy appropriate to its application. The stable sort in the
NumPy code makes repeated runs reproducible, but it does not prove that the
first tied row is more similar.
Identical points have distance zero
If , then and every norm gives . That is a valid exact match, not an error. In a training-set lookup, however, querying a row against the same fitted collection can return the row itself as the nearest neighbor. Decide whether self-matches are useful before interpreting a result; an external query that happens to duplicate a stored row should still receive distance zero.
Feature scaling is part of the geometry
Suppose one coordinate is annual income in dollars and another is a count from zero to ten. The dollar coordinate can dominate an L1 or L2 distance simply because its numeric unit is larger. Standardizing, rescaling, or assigning domain weights changes the geometry; it is not a cosmetic prelude. Fit any data-dependent scaling rule on the training data and apply the same rule to queries, or the neighbor map can leak information from the evaluation set.
Distance and cosine similarity answer different questions
Distance compares the displacement between two points and therefore includes their positions and magnitudes. Cosine similarity compares direction after normalizing lengths. For and ,
because the vectors point in exactly the same direction, while
An embedding-retrieval system may prefer cosine when direction is the intended notion of match. A scale-sensitive application may prefer a norm-induced distance. Neither choice is universally correct, and cosine similarity needs a separate policy for a zero vector because its direction is undefined.
High dimension calls for measurement, not a slogan
In some high-dimensional data distributions, nearest and farthest distances can become closer together, making the ranking less discriminative. Beyer, Goldstein, Ramakrishnan, and Shaft describe this distance-concentration result under specific conditions in their nearest-neighbor paper. That result is a warning about data and workload assumptions, not a law that every embedding space becomes useless. Inspect neighbor-distance gaps, validate the metric on held-out queries, and consider whether the representation contains many irrelevant coordinates before blaming the search algorithm.
The next lesson isolates the directional comparison that distance deliberately mixes with length: Lesson 044, Angles and Cosine Similarity. The Math for AI course page will show that lesson when its translation set is live.
Check your understanding
Question 1
For x = (5, −2) and y = (1, 1), compute the displacement and the L1, L2, and L-infinity distances.
Show the step-by-step solution
First subtract the points: . Apply each norm to that
one displacement. L1 is . L2 is
sqrt(4² + (−3)²) = sqrt(16 + 9) = 5. L-infinity is the largest
absolute coordinate, . The three distances are 7, 5, and 4
respectively.
Question 2
Why is ||x||₂ − ||y||₂ not a general formula for the distance between x and y? Demonstrate with x = (1, 1) and y = (1, −1).
Show the step-by-step solution
The separate lengths are both and , so their difference is zero. The actual displacement is , whose L2 norm is . Distance must subtract first because it measures the vector connecting the two locations, not the difference between their distances from the origin.
Question 3
Using q = (0, 0), A = (6, 0), B = (4, 4), and C = (5, 2), which two candidates are nearest under L-infinity? Show all three relevant distances.
Show the step-by-step solution
Subtracting leaves the candidate coordinates. The L-infinity distance keeps the largest absolute coordinate: , , and . Sorting 4, 5, and 6 gives first and second, so the two-neighbor result is .
Question 4
Under L1, q = (0, 0), u = (1, 1), and v = (2, 0) tie. What is the tie distance, and what must a k = 1 implementation decide?
Show the step-by-step solution
Compute each displacement from the origin. For , L1 gives ; for , it gives . The tied distance is therefore 2. A implementation must choose a policy: return both and let a later step resolve the tie, use a documented secondary key, or use an application rule. An array order may make the output deterministic, but it does not make one point mathematically closer.
Question 5
If a query x and stored point y are both (2, −1), what is their distance under every norm, and why is that result meaningful?
Show the step-by-step solution
The displacement is . Every norm maps the zero vector to zero, so for L1, L2, and L-infinity. This is the identity-of-indiscernibles property: identical points have no displacement. It is meaningful as an exact match, although a search over the training set may need an explicit self-match policy.
Question 6
Why can an unscaled feature dominate distance? Compare q = (0, 0), a = (1, 100), and b = (2, 0) with L2 distance.
Show the step-by-step solution
The displacements are and . Their L2 distances are and . The second coordinate makes look much farther even though its first coordinate is closer. Rescaling or standardizing features can make the coordinate contributions comparable, but that preprocessing rule must be fitted on training data and reused consistently.
Question 7
For x = (1, 1) and y = (10, 10), why can cosine similarity be 1 while L2 distance is not 0?
Show the step-by-step solution
The dot product is . The lengths are and , so cosine similarity is : the directions agree exactly. Distance keeps the magnitude difference. The displacement is , so . The two measures answer different similarity questions.
Question 8
In np.linalg.norm(points − query, ord=2, axis=1), what does axis=1 mean when points has shape (number_of_points, number_of_features)?
Show the step-by-step solution
Each row of points − query is one candidate displacement, and
each column is one feature coordinate. Setting axis=1 tells
NumPy to reduce across the columns of each row. The result therefore has one
L2 value per candidate, with shape
(number_of_points,). Reducing along the wrong axis would mix
candidates together or return one value per feature instead of a usable
neighbor ranking.