Cosine Similarity vs Euclidean Distance

Cosine similarity compares the directions of two vectors, while Euclidean distance measures the gap between them. Worked examples show when each one gives the sensible answer.
Statistics
Programming
Explainer
Author

Abdullah Al Mahmud

Published

December 1, 2026

Vectors u (2, 1) and v (20, 10) on the same line, 20.12 apart, with cosine similarity 1. Vector w (0, 2) is 2.24 from u, with cosine similarity 0.45.

The same three vectors give different answers: v is far from u but points the same way.

Cosine similarity looks at the angle between two vectors. Euclidean distance measures the straight-line distance between their end points. The first ignores the length of the vectors, while the second depends on it.

The two formulas

For two non-zero vectors \(\mathbf{u}\) and \(\mathbf{v}\) with \(n\) entries, cosine similarity is:

\[ \cos\theta = \frac{\mathbf{u} \cdot \mathbf{v}} {\lVert \mathbf{u} \rVert \, \lVert \mathbf{v} \rVert} \]

Euclidean distance is:

\[ d(\mathbf{u}, \mathbf{v}) = \sqrt{\sum_{i=1}^{n} (u_i - v_i)^2} \]

Note

They run in opposite directions. A higher cosine means more similar (1 is the same direction), while a higher distance means more different (0 is the same point).

Side by side

Feature Cosine similarity Euclidean distance
Captures Direction, relative proportions Absolute differences
Rescaling one vector by a positive number No effect Usually changes it
Range −1 to 1 (0 to 1 if no entry is negative) 0 upwards
Zero vector Undefined No problem

Example 1: document length

We count the words probability and sampling in three texts:

Text probability sampling Vector
A: short introduction 2 1 \(\mathbf{u} = (2, 1)\)
B: long chapter 20 10 \(\mathbf{v} = (20, 10)\)
C: note with another focus 0 2 \(\mathbf{w} = (0, 2)\)

A and B use the words in the same 2 : 1 ratio; B is just ten times longer.

Cosine similarity:

\[ \begin{aligned} \cos(\mathbf{u}, \mathbf{v}) &= \frac{2(20) + 1(10)}{\sqrt{5}\,\sqrt{500}} = \frac{50}{50} = 1 \\[6pt] \cos(\mathbf{u}, \mathbf{w}) &= \frac{2(0) + 1(2)}{\sqrt{5}\,\sqrt{4}} \approx 0.447 \end{aligned} \]

Euclidean distance:

\[ \begin{aligned} d(\mathbf{u}, \mathbf{v}) &= \sqrt{(2-20)^2 + (1-10)^2} \\ &= \sqrt{405} \approx 20.12 \\[6pt] d(\mathbf{u}, \mathbf{w}) &= \sqrt{(2-0)^2 + (1-2)^2} \\ &= \sqrt{5} \approx 2.24 \end{aligned} \]

By distance, A is nine times closer to C than to B. Cosine similarity gives the sensible answer here: A and B are about the same thing.

Example 2: physical measurements

Now we compare three patients by systolic blood pressure (mmHg) and fasting glucose (mg/dL):

Patient Pressure Glucose
P1: baseline 110 85
P2: both very high 220 170
P3: pressure slightly higher 125 85

Cosine similarity:

\[ \begin{aligned} \cos(\mathbf{p}_1, \mathbf{p}_2) &= 1 \\ \cos(\mathbf{p}_1, \mathbf{p}_3) &\approx 0.998 \end{aligned} \]

Euclidean distance:

\[ \begin{aligned} d(\mathbf{p}_1, \mathbf{p}_2) &= \sqrt{110^2 + 85^2} \approx 139.01 \\ d(\mathbf{p}_1, \mathbf{p}_3) &= \sqrt{15^2 + 0^2} = 15 \end{aligned} \]

Since \(\mathbf{p}_2 = 2\mathbf{p}_1\), cosine similarity treats a dangerous reading as identical to the baseline. Euclidean distance correctly puts P3 near P1 and P2 far away.

Warning

Euclidean distance depends on the units. With age in years and income in dollars, income would dominate. We usually standardise the features first (subtract the mean, then divide by the standard deviation).

What about many dimensions?

With many features, we meet the curse of dimensionality. For random data, most pairs of points end up at similar Euclidean distances, and most pairs of vectors are nearly at right angles, so their cosines are close to 0. Switching to cosine similarity is therefore not a cure by itself. Real data with some structure can still be compared usefully.

Choosing quickly

TipRule of thumb
  • Use cosine similarity when size is noise and proportions matter: word counts, TF-IDF scores, many text embeddings, user preferences.
  • Use Euclidean distance when size is information and the units are comparable: map coordinates, sensor readings, physical measurements, standardised tables (as in \(k\)-means clustering).

For data that is just presence or absence, see Jaccard vs cosine similarity.

References

R-bloggers

R posts from Stat Mania are also shared on R-bloggers, a hub of R blogs.