← Back to list

Cosine Similarity

What Cosine Similarity Is

rahularyansharma · 2026-05-21 07:10 · 0 claps · 1.1 min read
#cosine-similarity #vector #vector-search
Open on Medium ↗
Wiki topics: RAG · RAG & Retrieval

Cosine Similarity

What Cosine Similarity Is

Suppose you have two vectors. Maybe they’re word embeddings: one for “cat” and one for “kitten.” You want to know: how similar are these two meanings?

Cosine similarity measures it by computing the cosine of the angle between the two vectors.

cosine_similarity(A, B) = (A · B) / (|A| × |B|)

Where A · B is the dot product, and |A| is the length (magnitude) of vector A.

The result is always between -1 and +1:

  • +1: vectors point in exactly the same direction. Identical meaning (perfect similarity).
  • 0: vectors are perpendicular. Unrelated.
  • -1: vectors point in exactly opposite directions. Opposite meaning.

The Geometric Intuition

Forget magnitudes for a moment. Think of every vector as an arrow from the origin in some high-dimensional space.

  • “Cat” might point northeast.
  • “Kitten” might also point northeast, very close to “cat.”
  • “Spaceship” points southwest, far from both.

Cosine similarity asks: what’s the angle between two arrows? Small angle = similar. Big angle = different. It ignores how long the arrows are, only their direction.

Why Not Just Use Dot Products?

A dot product is A · B = |A| × |B| × cos(angle). It depends on both the angle AND the magnitudes. Two vectors might have a huge dot product just because they're both very long, even if their angle is wide.

Cosine similarity divides out the magnitudes, leaving only the angle. So it’s direction-only similarity, immune to scale.

This is useful when you don’t want vector magnitude to bias the comparison — like comparing word meanings, where a frequently used word might have a vector with bigger magnitude than a rare word, but you want them compared fairly on meaning.


메타데이터
post_id
ef979ff833a8
slug
cosine-similarity-ef979ff833a8
url
https://medium.com/@rahularyan786/cosine-similarity-ef979ff833a8
canonical_url
https://medium.com/@rahularyan786/cosine-similarity-ef979ff833a8
author_url
https://medium.com/@rahularyan786
status
ok
fetched_at
2026-06-09 15:37:30