[KongchangAI]
· 2 min read· 1,160 words

Why Cosine Similarity and Euclidean Distance Can Give Contradictory Results: Math Explained and a Practical Selection Guide

Why Cosine Similarity and Euclidean Distance Can Give Contradictory Results: Math Explained and a Practical Selection Guide

Cosine similarity measures direction only; Euclidean distance includes magnitude — L2 normalization makes them equivalent.

This article explores the mathematical roots of why cosine similarity and Euclidean distance can contradict each other in vector spaces. Using a simple 2D example, it shows how the two metrics can identify completely different "nearest neighbors." Cosine similarity normalizes away magnitude, making it ideal for semantic search and text embeddings, while Euclidean distance suits spatial clustering and magnitude-sensitive tasks. The key engineering insight: L2 normalization makes the two metrics strictly equivalent, which is why most vector retrieval systems normalize embeddings before indexing.

Introduction: The Hidden Conflict Between Two Distance Metrics

In machine learning, vector retrieval, and recommendation systems, measuring the "similarity" between vectors is one of the most fundamental operations. The two most widely used approaches are Cosine Similarity and Euclidean Distance. Intuitively, we tend to assume these two methods should agree — the closer two vectors are, the higher the cosine similarity and the smaller the Euclidean distance.

Reality tells a different story. In practice, cosine similarity and Euclidean distance can give completely opposite answers to the question of "which two vectors are more similar." Understanding the mathematical principles behind this contradiction is critical for building reliable retrieval systems and embedding models.

reddit source: Why cosine similarity and Euclidean distance can disagree

The Fundamental Difference Between Cosine Similarity and Euclidean Distance

Euclidean Distance: Cares About Absolute Position

Euclidean distance measures the straight-line distance between two vectors in space. For vectors A and B, the formula is:

d(A, B) = √(Σ(Aᵢ - Bᵢ)²)

Euclidean distance is highly sensitive to the magnitude of vectors. Even if two vectors point in exactly the same direction, a large difference in their lengths (norms) will result in a significantly larger Euclidean distance.

Cosine Similarity: Cares Only About Direction

Cosine similarity measures the cosine of the angle between two vectors, calculated as:

cos(θ) = (A · B) / (||A|| × ||B||)

The key is the normalization in the denominator — cosine similarity completely ignores magnitude and only considers direction. Two vectors pointing in exactly the same direction but with vastly different lengths still have a cosine similarity of 1 (perfectly similar).

In short: Euclidean distance accounts for both direction and magnitude, while cosine similarity only looks at direction. This fundamental difference is the root cause of their contradictions.

How Contradictions Arise

A Concrete Example Demonstrating the Magnitude-Driven Conflict

Consider three 2D vectors:

  • A = (1, 1)
  • B = (2, 2)
  • C = (1, 0)

For vector A:

  • A and B point in exactly the same direction (both on the 45° line), so their cosine similarity is 1, but their Euclidean distance is approximately 1.41.
  • A and C point in different directions (45° apart), so their cosine similarity is approximately 0.707, but their Euclidean distance is exactly 1.

The contradiction is clear:

MetricA vs. BA vs. CConclusion
Cosine Similarity1 (more similar)0.707A is closer to B
Euclidean Distance1.411 (closer)A is closer to C

The same three vectors, and the two metrics give completely opposite answers about the "nearest neighbor."

The Root Cause: Normalization

The core reason for this divergence is that cosine similarity implicitly normalizes vectors to unit length, while Euclidean distance preserves the original magnitude information. When vectors in a dataset vary significantly in norm, the two metrics fundamentally split their focus — one only looks at angle, the other gets stretched by the difference in scale.

A Practical Guide to Choosing the Right Metric

When to Use Cosine Similarity

In scenarios like text embeddings, semantic retrieval, and recommendation systems, we typically care more about the "semantic direction" of content than its intensity. For example, two documents of different lengths but on the same topic should have embedding vectors with similar directions, even if their norms differ significantly. Cosine similarity better matches business intuition here, which is why most vector databases (such as those used in RAG retrieval systems) default to cosine similarity.

When to Use Euclidean Distance

In scenarios like spatial clustering (e.g., K-Means), image feature matching, and anomaly detection — where magnitude itself carries important information — Euclidean distance is more appropriate. When each dimension of a vector represents a comparable physical quantity or absolute value, ignoring magnitude can actually discard critical information.

It's worth noting that the standard K-Means clustering algorithm relies on Euclidean distance by design — each iteration computes the Euclidean distance from each sample to each cluster centroid and assigns membership accordingly. Running K-Means directly on high-dimensional text embeddings can be problematic: differences in embedding norms across documents may cause clusters to be dominated by a few vectors with very large magnitudes, resulting in semantically similar documents being split across different clusters. The solution is to apply L2 normalization before clustering, or to use a Spherical K-Means variant that uses cosine distance instead. In anomaly detection, the deviation in magnitude itself is often the anomaly signal (e.g., a sensor reading that is globally elevated), making Euclidean distance better suited than cosine similarity for capturing such anomalies.

The Key Technique for Eliminating the Contradiction: L2 Normalization

A highly practical technique is this: if you normalize all vectors to unit length (L2 normalization) beforehand, Euclidean distance and cosine similarity become equivalent, with a strict monotonic relationship between them. Specifically, for unit vectors:

||A - B||² = 2(1 - cos θ)

The squared Euclidean distance equals 2(1 - cosθ) — the higher the cosine similarity, the smaller the Euclidean distance. The two metrics are in perfect agreement.

Therefore, many vector retrieval systems apply L2 normalization to embedding vectors before indexing, ensuring both metrics remain consistent and avoiding unexpected results from the contradiction described above.

The derivation of L2 normalization can help deepen the intuition. For two unit vectors A and B (i.e., ||A|| = ||B|| = 1), expand the squared Euclidean distance:

||A - B||² = (A - B)·(A - B)
           = A·A - 2A·B + B·B
           = 1 - 2cos θ + 1
           = 2(1 - cos θ)

This means that on the unit hypersphere, Euclidean distance is a strictly monotonic function of cosine similarity. In practice, leading vector databases (such as Faiss, Milvus, and Pinecone) typically note in their documentation that using an Inner Product index with L2-normalized vectors is equivalent to cosine similarity retrieval — and this approach can often reuse efficient approximate indexing structures like HNSW and IVF without needing a separate index built specifically for cosine similarity.

Summary

The contradiction between cosine similarity and Euclidean distance is not a flaw in either algorithm — it's an inevitable consequence of their mathematical definitions. One only looks at direction; the other accounts for both direction and magnitude. Understanding this distinction helps us:

  • Choose the right similarity metric for the use case: semantic retrieval favors cosine similarity, spatial clustering favors Euclidean distance
  • Unify the behavior of both metrics through L2 normalization, eliminating interference from magnitude differences
  • Avoid hard-to-explain "wrong neighbors" in vector retrieval and recommendation systems

For developers working with embedding models and vector retrieval, this is a detail that seems basic but is surprisingly easy to overlook. Before designing a system, first clarify whether magnitude carries meaningful information in your data, then choose the appropriate metric — it can save a significant amount of debugging effort down the line.

#cosine similarity#Euclidean distance#vector similarity#L2 normalization#embedding retrieval#semantic search#vector database#K-Means clustering
Share:

Related articles