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]Output0.816Angle of about 35°
Common errors
- Unnormalized vectors cause computation overflow
- Apply L2 normalization in advance or use a numerically stable implementation
Related terms
TF-IDF
TF-IDF (term frequency-inverse document frequency) measures how important a word is to a document: words frequent in the document but rare in the corpus receive high weights. GLOSSARY
Bag of Words
The bag-of-words model represents text as an unordered word-frequency vector, ignoring grammar and word order; it is the foundational representation for text classification and retrieval. GLOSSARY
Jaro-Winkler Distance
The Jaro-Winkler distance is a refinement of the Jaro distance that weights common prefixes, making it better suited for matching short strings such as person names where prefixes matter. GLOSSARY