Understanding Cosine Similarity
Cosine Similarity:
cos(θ) = (A · B) / (|A| × |B|)
Ranges from −1 (opposite direction) to 1 (same direction)
Cosine similarity measures how similar two vectors are in direction, independent of their magnitude (length). It is the cosine of the angle θ between vector A and vector B in whatever-dimensional space they occupy. The formula divides the dot product of the two vectors by the product of their magnitudes (Euclidean norms), which cancels out scale — a vector and a longer copy of itself pointing the same way still score a perfect 1.
The pieces of the formula
- Dot product (A · B): multiply each pair of matching components and add the results — for 3D vectors, A·B = axbx + ayby + azbz.
- Magnitude (|A|): the vector's Euclidean length, |A| = √(ax² + ay² + az²), and likewise for |B|.
- Angle (θ): once cos(θ) is known, the angle itself is θ = arccos(cos(θ)), usually reported in degrees.
This calculator works with two 3-dimensional vectors. To compare 2D vectors, leave both Z components at 0 — the extra dimension contributes nothing to the dot product or the magnitudes, so the result is identical to a true 2D calculation.
Reading the result
A value of 1 means the vectors point in exactly the same direction (perfectly similar). A value of 0 means they are orthogonal — perpendicular, with no directional relationship. A value of −1 means they point in exactly opposite directions. Values in between indicate partial similarity: the closer to 1, the more the vectors align.
Common applications
- Text and document similarity: comparing word-frequency or embedding vectors to find similar documents, articles, or search results.
- Recommendation systems: comparing user or item feature vectors to suggest similar products, movies, or content.
- Machine learning and NLP: measuring closeness between word or sentence embeddings produced by models.
- Image and audio matching: comparing feature vectors extracted from images or audio clips for similarity search.
Cosine similarity is popular for these tasks because it ignores magnitude: a short document and a long document that use the same words in the same proportions will still score close to 1, which is usually the desired behavior when magnitude (like document length) is not itself meaningful.