Model

Cosine Similarity

Why Cosine Similarity Is Needed

Embedding turns text into vectors, but once you have two vectors, how do you tell how "similar" they are?

For example, vector A = [0.8, 0.2, 0.5], vector B = [0.7, 0.3, 0.4] — is this similar or not? Comparing raw values directly is messy — different dimensions may have vastly different scales.

Cosine Similarity solves the problem of "how to quantify similarity between vectors."


What Is Cosine Similarity

One-line definition: Cosine Similarity measures how close two vectors are in "direction," with values closer to 1 indicating greater similarity.

Why is it called "cosine"? Because its formula is equivalent to calculating the cosine of the angle between two vectors.

Analogy: Two vectors are like arrows pointing in different directions. Cosine similarity measures how much these two arrows "point in the same direction":

  • Pointing in exactly the same direction → cosine value = 1
  • Perpendicular → cosine value = 0
  • Opposite directions → cosine value = -1

In Embedding scenarios, "pointing in the same direction" = "semantically similar."


How to Do It: When to Use Cosine Similarity

Scenarios suited for Cosine Similarity:

  • Semantic search ranking: return documents sorted by similarity score
  • Similar text matching: finding duplicate content, similar questions, similar products
  • Vector clustering: grouping by semantic similarity

Scenarios not suited for Cosine Similarity:

  • Exact ID, order number, or name matching — keyword search is better here
  • When vectors aren't used to represent semantics (e.g., vectors for other purposes)

Common pitfalls:

  • High similarity doesn't mean correct answer: only indicates semantic closeness, the content itself may have problems
  • Small score differences matter: near ranking boundaries, tiny differences can affect the top results

Remember this: Cosine Similarity measures how close vectors are in "direction," not "magnitude" — in Embedding scenarios, close direction = semantically similar.

Related terms: Embedding · Vector Database