# Cosine Similarity

Cosine similarity is a measure used to determine how similar two non-zero vectors are in their orientation in a multi-dimensional space, irrespective of their magnitude. It's widely used in various fields, including data analysis, information retrieval, and natural language processing, to measure the cosine of the angle between two vectors.

### Mathematical Definition:

The cosine similarity between two vectors $A$ and $B$ is defined as:

$\text{cosine\_similarity}(A, B) = \frac{A \cdot B}{\|A\|\|B\|}$

Where:

• $A \cdot B$ represents the dot product of vectors $A$ and $B$.
• $\|A\|$ and $\|B\|$ are the Euclidean norms (or magnitudes) of vectors $A$ and $B$, respectively.

### Dot Product:

The dot product of two vectors is a scalar that represents the sum of the products of their corresponding components:

$A \cdot B = \sum_{i=1}^{n} A_i B_i$

### Euclidean Norm:

The Euclidean norm (or magnitude) of a vector is the square root of the sum of the squares of its components:

$\|A\| = \sqrt{\sum_{i=1}^{n} A_i^2}$

### Cosine of the Angle:

The cosine similarity essentially measures the cosine of the angle between two vectors. The cosine of 0° is 1, and it is less than 1 for any other angle. It is maximally 1 when the two vectors point in the same direction, 0 when they are perpendicular, and minimally -1 when they point in opposite directions.

### Interpretation:

• 1: Vectors are identical in orientation.
• 0: Vectors are orthogonal (perpendicular) to each other.
• -1: Vectors are diametrically opposite.

### Applications:

1. Document Similarity: In text analysis, documents are often represented as vectors of term frequencies (TF-IDF, for example). Cosine similarity can be used to find how similar the documents are irrespective of their size.
2. Collaborative Filtering: In recommendation systems, it's used to measure the similarity between users or items based on their preferences or behavior.
3. Clustering: In machine learning, cosine similarity is used as a metric to measure how similar the data points are to one another in clustering algorithms.