AI Grounds

Open AI Grounds on a desktop

These interactive lessons need a larger screen. Please continue on a desktop or laptop computer.

Guided discovery

Distance Metrics Lab

Move a query; compare what closest means.

Your dataset

Four fixed labeled reference cases A..D and one query with no known true label. One-nearest-neighbor (1-NN) uses the unique closest reference’s label. This lesson withholds a decision for every nonunique nearest neighbor.

Distance metric

00224466881010X coordinateY coordinateABCD

● Circle class■ Square class◇ Query (movable)Ring = nearest

Drag the query diamond to an integer grid point, or focus it and use arrows (Shift: two units). Up increases Y; Right increases X. Home sets (0,0); End sets (10,10). Enter or click focuses Query X. Reference cases stay fixed.

Horizontal query coordinate, 0..10 in whole units. Use arrow keys on the slider. Press Enter or leave the number field to apply an exact edit.

Vertical query coordinate, 0..10 in whole units. Use arrow keys on the slider. Press Enter or leave the number field to apply an exact edit.

Euclidean · Query (0, 0) · Nearest B · Minimum 4.242641 · Square class decision

Euclidean = sqrt(dx² + dy²): the straight-line length. Both axes have equal numerical weight. Distance is not a class probability.

Nearest case(s)

All cases at the minimum chosen distance.

B

Compare exact values before rounding.

IDs remain distinct even at equal coordinates.

Minimum distance

Smallest Euclidean distance to the query.

4.243

sqrt(dx² + dy²)

Equal numerical weights; no true query label.

1-NN decision

Stored label of the unique nearest reference.

Square

This lesson’s declared tie policy.

A label decision does not establish accuracy.

Case distances and decisions
All four fixed references, separate even when coincident. dx/dy are absolute differences from query (0,0). Euclidean = sqrt(dx²+dy²); Manhattan = dx+dy. Distances are rounded to six decimals; exact values decide nearest ties.
CaseCoordinatesClass|ΔX||ΔY|EuclideanManhattanNearest
A(0, 5)Circle055.0000005.000000No
B(3, 3)Square334.2426416.000000Yes
C(8, 9)Circle8912.04159517.000000No
D(9, 1)Square919.05538510.000000No
Construction and limits

Metric disagreement: A (0,5), B (3,3), C (8,9), D (9,1). Tie boundary: A (2,5), B (8,5), C (5,9), D (5,1). Coincident cases: A (2,2), B (2,2), C (8,8), D (8,2). A/C are Circle, B/D Square. Changing scenario preserves query and metric; changing prediction or Reset restores the current experiment baseline. Query, metric and scenario changes clear stale explanations.

The two coordinates count equally as numbers. Euclidean compares exact integer squared distances before taking square roots; Manhattan compares integer absolute sums. Equal plotting scales preserve numerical geometry. The straight Euclidean segment or one Manhattan grid route is shown to every nearest case. Zero-distance paths legitimately have zero length. A combined A/B circle/square mark and separate table rows retain coincident identities; the query can overlap them at the same true position.

1-NN assigns the unique nearest reference’s stored class. This lesson withholds a decision for any nonunique minimum, even if the tied labels agree. That explicit policy is not universal: other implementations may choose by order or another declared rule. Identical coordinates with different labels remain tied under both metrics. No known query label or performance evaluation establishes which metric is appropriate for a real task.

The finite 0..10 integer toy excludes k>1 voting, reference editing, learned weights, feature scaling, obstacles, training optimization, class probabilities and automatic metric choice. Different numeric units could change the meaning of equal weights; no physical mixed-unit distance or universal accuracy guarantee is implied.

SciPy · Euclidean distance
SciPy · Manhattan distance
scikit-learn · Nearest neighbors and tie behavior