Skip to article frontmatterSkip to article content

3.4 Distance and Nearest Neighbors

Howdy neighbor!

Dept. of Electrical and Systems Engineering
University of Pennsylvania

Binder

Lecture notes

1Reading

Material related to this page, as well as additional exercises, can be found in VMLS 3.2.

2Learning Objectives

By the end of this page, you should know:

  • the Euclidean distance between two vectors
  • the properties of a general distance function

3The Euclidean Distance

A distance function, or metric, describes how far apart 2 points are.

A familiar starting point for our study of distances will be the Euclidean distance, which is closely related to the Euclidean norm on Rn\mathbb R^n:

Note that this is measuring the length of the arrow drawn from point x\vv x to point y\vv y:

Euclidean distance bewteen 2 vectors $\vv x$ and $\vv y$

3.1Python break!

We use the np.linalg.norm function in Python to compute the Eucledian distance between two vectors by computing the norm of the difference between the vectors.

# Distance between vectors
import numpy as np

v1 = np.array([1, 2])
v2 = np.array([3, 4])
euc_dist = np.linalg.norm(v1 - v2)
print("Eucledian distance: ", euc_dist)
Eucledian distance:  2.8284271247461903

4General Distances

In this course, we will only work with the Euclidean distance. However, given any vector space with a general norm (i.e., Rn\mathbb{R}^n with the Euclidean norm), we may construct a distance function as the norm of their difference. This leads us to a more general notion of distances:

Try to convince yourself why the Euclidean distance fits this definition.

When the distance xy\| \vv x - \vv y \| between two vectors x,yV\vv x, \vv y \in V is small, we say they are “close.” If the distance between xy\| \vv x - \vv y \| is large, we say they are “far.” What constitutes close or far is typically application dependent.

Note that one vector space can admit many distance functions. From here on, unless otherwise mentioned, we will only be considering the Euclidean distance.

5Applications of Distances

Binder