What is Cosine Similarity?

Cosine similarity is a metric that measures the cosine of the angle between two vectors in a multidimensional space.

Unlike Euclidean or Manhattan distance, cosine similarity focuses on the orientation of the vectors rather than their magnitude.

image.png

Mathematically, cosine similarity between two vectors A and B is defined as:

$$ Cosine Similarity= \cos(\theta) = \frac{A \cdot B}{\|A\| \|B\|} $$

Where:

The value of cosine similarity ranges from −1 to 1, where 1 indicates identical orientation, 0 indicates orthogonality, and −1 indicates opposite directions.

Explanation

The ‘magnitude’ of a vector is a measure of its length in the vector space.

For a vector $A=(a1,a2,…,an)$, its magnitude $∥A∥$ is calculated as:

$$ ||A|| = \sqrt{a_1^2 + a_2^2 + \dots + a_n^2} $$

Application

Cosine similarity is commonly used in text and document clustering, where features represent word counts, TF-IDF scores, or other vectorised data.

Here, the magnitude of the vectors (e.g., document length) is less important than their direction (e.g., relative word frequency). Algorithms such as K-means or hierarchical clustering can be adapted to use cosine similarity instead of Euclidean distance.

Cosine similarity is particularly useful when our data is sparse or high-dimensional, as it effectively compares the relative contributions of dimensions rather than absolute differences. For instance, in text mining, cosine similarity can identify documents with similar topics even if they differ significantly in word counts.