AI Glossary / Mathematics and optimization / Advanced
Distance
Definition
Distance is a numerical measure of separation between two objects. In the mathematical sense used here, it is the value of a metric: a function that is nonnegative, equals zero exactly for identical points, is symmetric and satisfies the triangle inequality. Machine-learning applications also use measures called distances more loosely, so their mathematical properties should be checked rather than inferred from the name.
What it means
A distance tells a method what it should regard as close or far apart. The objects might be coordinate vectors, strings or vertices in a graph. Their representation and the chosen distance jointly determine the comparison.
There is no universal notion of closeness that automatically matches every business question. Two customers can be close in recent spending but far apart in service needs. Likewise, numerically nearby document representations need not answer the same question. Distance is meaningful relative to the features, units and comparison rule selected for the task.
How it works
Euclidean distance between vectors is the square root of the sum of squared coordinate differences. Manhattan distance sums their absolute differences. Both are metrics on ordinary real coordinate vectors, but they describe different geometries and can rank candidate neighbors differently.
The triangle inequality states that d(x, z) ≤ d(x, y) + d(y, z). It says that an intermediate stop cannot make the direct metric distance exceed the combined two-part distance. Some algorithms rely on this property to organize or eliminate comparisons. A useful dissimilarity measure need not satisfy it, but using such a measure in an algorithm that assumes a metric requires justification.
Three practical examples
- A nearest-neighbor classifier compares a new feature vector with stored examples. Changing the distance definition can change the chosen neighbors and therefore the prediction, even when the training records remain identical.
- A route-analysis tool measures shortest-path distance on a connected, undirected graph with positive edge lengths. That metric follows available connections rather than straight lines on a map. Directed routes can have different outward and return distances.
- A clustering system groups numerical observations using a chosen geometry. Feature scaling can change which observations appear close, so the analyst documents preprocessing together with the distance rather than treating them as unrelated implementation details.
Think of it this way
Think of measuring separation either across an open field or along a rectangular street grid. Straight-line travel and travel restricted to horizontal and vertical streets answer different questions. Neither is wrong simply because the answers differ. The mistake is to use one measurement while interpreting the result as though the other had been used.
Worked example
Let a query point be q = (0, 0), with candidates a = (3, 0) and b = (2, 2). Treat the coordinates as two numerical features on specified scales.
Under Euclidean distance, d(q, a) = √(3² + 0²) = 3. For b, d(q, b) = √(2² + 2²) = √8, approximately 2.828. Candidate b is the nearer point.
Under Manhattan distance, the distances are |3| + |0| = 3 for a and |2| + |2| = 4 for b. Candidate a is now nearer. The points did not move; the comparison rule changed.
Next keep Euclidean distance but multiply the second feature by two. The transformed query and a stay unchanged, while b becomes (2, 4). Its new distance is √(2² + 4²) = √20, approximately 4.472. Candidate a, still at distance three, becomes nearer. Rescaling a feature has changed its influence on the comparison.
Finally, consider squared Euclidean dissimilarity on the real line for points 0, 1 and 2. The squared distance from 0 to 2 is four, while the two squared distances through 1 sum to one plus one, or two. Since four exceeds two, the triangle inequality fails. Squaring preserves the ordering of individual nonnegative distances from a fixed query, but it does not preserve every metric property.
Illustrative case study
In a fictional service-planning project, customer records contain monthly spending and the number of support requests. Spending is stored in whole currency units. An analyst applies ordinary Euclidean distance directly and notices that spending differences dominate most comparisons.
Another analyst changes the spending column to thousands of currency units without changing the underlying customer information. The neighbor lists change substantially because the numerical scale changed. The team recognizes that the original result depended on a unit choice that had never been discussed as part of the model.
It defines a documented scaling approach using training data, evaluates whether the resulting neighbors support the intended service task and applies the fitted transformation consistently to new records. The team does not assume that equal numerical influence is always best. It treats scaling and distance selection as modeling choices whose usefulness must be evaluated against the actual objective.
Common misconceptions and mistakes
A small distance does not guarantee meaningful similarity, correct classification or shared causation. The representation may omit important differences or emphasize irrelevant ones. A zero distance between identical feature vectors also does not prove that two source records describe the same real-world object.
Not every quantity named a distance is a metric, and not every measure of model performance is a distance between data points. Squared Euclidean dissimilarity provides a concrete triangle-inequality counterexample. Shortest-path distance also needs care: disconnected graph vertices may have no finite connecting distance, while directed paths generally do not ensure symmetry.
Business use
Specify what objects are compared, what features describe them and what distance definition is used. Record units, scaling and any feature weights. Test whether nearby examples are useful for the actual decision, not merely visually plausible on a plot. If changing units changes the result, determine whether that effect is intended. Keep preprocessing fitted on development data separate from held-out evaluation to preserve a meaningful assessment.
People also ask
Can two valid distances choose different nearest neighbors?
Yes; Euclidean and Manhattan distance do so in the worked example.
Is squared Euclidean distance a metric?
In general, no, because it can violate the triangle inequality.
Does a distance of zero identify the same customer?
Only identical represented points under the metric, not necessarily the same source entity.
Check your understanding
Answer: Which candidate is Euclidean-nearest before rescaling? b, at distance √8. Which is Manhattan-nearest? a, at distance three.
Why does b become farther after rescaling?
Answer: Doubling its second coordinate increases that feature's contribution to Euclidean distance.
Related terms
- Metric
- Metric space
- Euclidean distance
- Manhattan distance
- Nearest neighbor
- Triangle inequality
- feature scaling
- dissimilarity
Learn next
Compare several distance definitions on the same small dataset. Then study how feature transformations and algorithm assumptions affect nearest-neighbor and clustering results.
More in Mathematics and optimization
- Absolute error loss - Absolute error loss measures a numerical prediction's error as the absolute difference between its predicted and reference values: L(y,y-hat)=|y-y-hat|.
- Active-set method - An active-set method is an optimization approach that works with a changing subset of constraints believed to be tight at the solution.
- Adjoint state method - The adjoint state method computes how a chosen objective changes when the parameters of a mathematical model change.
- Σ-algebra
- Algebraic connectivity - Algebraic connectivity is the second-smallest eigenvalue of a graph's Laplacian matrix.
- Algorithm IMED - Algorithm IMED, short for Indexed Minimum Empirical Divergence, is a method for choosing actions in a stochastic multi-armed bandit problem.
Sources
Aalto Dictionary of Machine Learning, distance, metric and metric space: metric properties and examples. https://aaltodictionaryofml.github.io/ADictML_Main.pdf SciPy, scipy.spatial.distance.euclidean: Euclidean distance between numerical vectors. https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.euclidean.html The neighbor comparisons, squared-distance counterexample and fictional service-planning case are original teaching illustrations.
Previous: Dispersive flies optimization Next: Distance map