Euclidean Distance

Another way to compare two embedding vectors is the plain straight-line distance between them β€” Euclidean distance. It's the most intuitive measure of all: literally how far apart two points are in space. Unlike cosine similarity, which only cares about direction, Euclidean distance takes the vectors' magnitude into account.

πŸ’‘ In one line: Euclidean distance is the straight-line gap between two vectors β€” smaller means more similar.

What is Euclidean Distance?

It's the "as the crow flies" distance between two points (vectors). A distance of 0 means they're identical; larger distances mean they're more different. Note the direction of the scale: this is a distance, so lower = more similar (the opposite of a similarity score).

The Formula

d(A,B)=βˆ‘i(aiβˆ’bi)2d(A, B) = \sqrt{\sum_i (a_i - b_i)^2}

It's the length of the difference vector β€” the Pythagorean theorem extended to many dimensions.

Interpreting Distance

  • 0 β€” identical vectors.
  • Small β€” similar.
  • Large β€” dissimilar.

Because it's a distance, you often rank ascending (nearest first) β€” the opposite of ranking a similarity descending.

Euclidean vs. Cosine

Euclidean distanceCosine similarity
MeasuresStraight-line gapAngle between vectors
MagnitudeMattersIgnored
Best whenAbsolute position mattersOnly direction/meaning matters

A useful fact: on normalised (unit-length) vectors, the two rank items the same way. On raw vectors they can differ β€” which is why cosine is usually preferred for text (where length shouldn't matter).

Example

Illustrative distances (smaller = closer):

  • "cat" vs "kitten" β†’ 0.4 (close)
  • "cat" vs "car" β†’ 1.8 (far)

When to Use It

  • When magnitude / absolute position genuinely matters.
  • Clustering β€” algorithms like k-means use Euclidean distance.
  • Some vector databases index by an L2 (Euclidean) metric.

Caveat: in very high dimensions, Euclidean distances can become less discriminative (the "curse of dimensionality"), which is another reason cosine is common for text embeddings.

Distance to Similarity

Since many systems want a similarity score, you can convert a distance:

similarity = 1 / (1 + distance)


Smaller distance β†’ higher similarity.

Summary

  • Euclidean distance is the straight-line gap between two vectors.
  • Formula: the length of the difference vector (Pythagoras in many dimensions).
  • 0 = identical; smaller = more similar (rank ascending).
  • Unlike cosine, it uses magnitude β€” good when absolute position matters (e.g. k-means).
  • On normalised vectors it ranks like cosine; for text, cosine is often preferred. EOF echo created