Cosine Similarity

Measures the angle between two vectors rather than the distance, ranging from −1-1 (opposite) to +1+1 (identical direction) and ignoring vector length. For non-negative feature vectors it runs from 0 to 1.

When to use

Use it for recommenders and text, where vectors are sparse and users have personal rating scales: two users rating 4 to 5 and 2 to 3 are far apart by Euclidean Distance but near-identical by cosine. The formula is cosine(a,b)=a⋅b∥a∥ ∥b∥\text{cosine}(\mathbf{a}, \mathbf{b}) = \dfrac{\mathbf{a} \cdot \mathbf{b}}{\lVert \mathbf{a} \rVert \, \lVert \mathbf{b} \rVert}.

See Lec 6.

Appears in