Cosine similarity
Cosine similarity of two nonzero real vectors is their dot product divided by the product of their Euclidean lengths.
On this page 8 sections
01For u=(5,0) and v=(3,4), the dot product is 15#
For and , the dot product is 15. Doubling doubles that score to 30, while the angle stays the same.
Cosine similarity divides out both lengths. For and , each length is 5, giving .
For and , find cosine similarity.
Show answer and explanation
The dot is 15 and the lengths are 5, so .
02A closer look#
The score is 1 for the same direction, 0 for perpendicular vectors, and −1 for opposite directions. Positive scaling leaves the score unchanged because it scales the dot product and the length together.
Cosine is requested for and . What happens?
Show answer and explanation
The zero vector makes the denominator zero.
03Check your understanding#
Cosine search uses . Which scores higher: or ?
Show answer and explanation
Both lengths are 5. Scores are and .
04Check your understanding#
A learner divides dot product 8 by lengths . What should replace that sum?
Show answer and explanation
Cosine uses the product of the two lengths.
Divide the dot product by both lengths; zero vectors are excluded.
- Compute directional similarity for two nonzero vectors.
Sources & further reading
- [1]OpenStax Calculus Volume 3: The Dot Product ↗openstax.org · Article
- [2]Manning, Raghavan and Schütze: Dot products ↗nlp.stanford.edu · Article