Report

Help us improve this tool

Cosine Similarity Calculator

Calculate cosine similarity, cosine distance, dot product, and angle between 2D, 3D, and n-dimensional vectors.

O M T

What is Cosine Similarity?

Cosine similarity is a metric used to measure how similar two non-zero vectors are in an inner product space. Unlike Euclidean distance, which measures the straight-line physical separation between vector points in coordinate space, cosine similarity measures the cosine of the angle ($\theta$) between the two vectors, focusing strictly on their directional orientation rather than their magnitude.

Mathematical Formula for Cosine Similarity

For two $n$-dimensional vectors $\mathbf{A} = (A_1, A_2, \dots, A_n)$ and $\mathbf{B} = (B_1, B_2, \dots, B_n)$, the cosine similarity is defined as the dot product divided by the product of their Euclidean lengths (L2 norms):

$$\text{Cosine Similarity} = \cos(\theta) = \frac{\mathbf{A} \cdot \mathbf{B}}{\|\mathbf{A}\| \|\mathbf{B}\|} = \frac{\sum_{i=1}^n A_i B_i}{\sqrt{\sum_{i=1}^n A_i^2} \sqrt{\sum_{i=1}^n B_i^2}}$$

Cosine Distance

The complementary measure, cosine distance, represents the angular dissimilarity between the vectors and is defined as:

$$D_C(\mathbf{A}, \mathbf{B}) = 1 - \cos(\theta)$$

Cosine distance ranges from 0 (for identical vector directions) to 2 (for vectors pointing in directly opposite directions).

Interpretation of Cosine Similarity Values

  • $\cos(\theta) = 1$ ($\theta = 0^\circ$): The vectors point in the exact same direction (perfect collinearity).
  • $0 < \cos(\theta) < 1$ ($0^\circ < \theta < 90^\circ$): Positive correlation and similarity between the vectors.
  • $\cos(\theta) = 0$ ($\theta = 90^\circ$): The vectors are orthogonal (perpendicular), indicating no linear correlation or mutual overlap.
  • $-1 < \cos(\theta) < 0$ ($90^\circ < \theta < 180^\circ$): Negative correlation (dissimilar directions).
  • $\cos(\theta) = -1$ ($\theta = 180^\circ$): The vectors point in directly opposite directions.

Applications in Data Science and Natural Language Processing

Cosine similarity is a cornerstone algorithm across modern computational domains:

  • Natural Language Processing (NLP): Comparing text documents, TF-IDF term frequency vectors, and word embedding representations (e.g. Word2Vec, GloVe, BERT, OpenAI embeddings).
  • Recommendation Systems: Finding users or products with similar rating profiles (collaborative filtering).
  • Vector Search & AI: Powering semantic retrieval and retrieval-augmented generation (RAG) in vector databases like Pinecone, Milvus, and pgvector.

Related Mathematical Calculators

Explore related vector mathematics and trigonometry tools:

Frequently Asked Questions

Can cosine similarity be negative?

Yes. If vectors contain negative components and the angle between them is obtuse ($\theta > 90^\circ$), their dot product is negative and the cosine similarity will fall between 0 and -1. In text analysis where term frequencies are always non-negative ($A_i \ge 0$), cosine similarity is naturally bounded between 0 and 1.

Why is cosine similarity preferred over Euclidean distance for text analysis?

Euclidean distance is sensitive to document length; a short 100-word article and a 1,000-word essay on the exact same topic would have a large Euclidean distance due to word frequency scale differences. Cosine similarity normalizes vector lengths, focusing exclusively on relative topic distribution and word co-occurrence.

What happens when one of the vectors is a zero vector?

The magnitude of a zero vector is zero ($\|\mathbf{0}\| = 0$). Because division by zero is mathematically undefined, cosine similarity cannot be computed for zero vectors.

How is the angle between two vectors calculated from cosine similarity?

The angle $\theta$ is found by taking the inverse cosine (arccosine) of the similarity score: $\theta = \arccos(\text{Cosine Similarity})$.

Does cosine similarity satisfy triangle inequality?

Cosine distance ($1 - \cos\theta$) does not satisfy the triangle inequality and is therefore not a formal mathematical metric. However, angular distance ($\arccos(\cos\theta)/\pi$) is a proper metric that satisfies all metric space requirements.