Skip to content
uencoder

Cosine Similarity

Cosine similarity measures the cosine of the angle between two vectors, ignoring vector length; it is widely used for text-vector similarity computation.

Purpose

Compares vector direction rather than magnitude, allowing long and short documents to be compared fairly.

Mechanism

sim(A,B) = (A·B) / (|A|×|B|) = Σ(A_i×B_i) / (sqrt(ΣA_i²)×sqrt(ΣB_i²)). Output ranges from -1 to 1 (0 to 1 for non-negative text vectors). 1 means identical direction; 0 means orthogonal (unrelated).

Use cases

  • Document similarity retrieval
  • Item similarity in recommender systems
  • Semantic similarity of word vectors
  • Clustering distance metric

Examples

  • Input
    [1,0,1] vs [1,1,1]
    Output
    0.816

    Angle of about 35°

Common errors

Unnormalized vectors cause computation overflow
Apply L2 normalization in advance or use a numerically stable implementation

Related terms

Related tools

References