Use cosine similarity to compare directions
Preserve the order of coordinates and the signs of values. · 172 words
Cosine similarity measures how closely two vectors point in the same direction, regardless of length. It divides the dot product by the product of their norms. A dot product multiplies matching entries and adds them. A norm is a vector's length. For a book recommender, let a reader's interest vector be (1,2,0) and a book's vector be (2,4,0). Their dot product is 1*2+2*4=10. Their norms are sqrt(5) and sqrt(20), whose product is 10. Thus cosine similarity is 10/10=1: both vectors have the same direction, although the book vector is longer.
A document search system can also compare feature vectors. Suppose two article summaries are represented by (1,1,0) and (1,0,1), where each entry counts a topic. Their dot product is 1, and both norms are sqrt(2), so cosine similarity is 1/(sqrt(2)*sqrt(2))=1/2. The value 0.5 says they share some direction but are not closely aligned. By convention, 1 means identical direction, 0 means perpendicular, and -1 means opposite directions. Because length is removed, this score is useful when two records have different feature magnitudes.
Check your recall
What is the cosine similarity of the vectors (1,0) and (1,1)?
Check your answer
Their dot product is 1, and their norms are 1 and sqrt(2), so the cosine similarity is 1/sqrt(2), approximately 0.707.
Sources and credits
Original KeyTouch lesson. Background reading · Zhang et al. · Linear Algebra.