Cosine Similarity Calculator – Guide & Formulas
Calculate the cosine similarity between two vectors instantly. Enter vector components to see the similarity score, angle, and step-by-step dot product computation.
Calculate the cosine similarity between two vectors with our free online calculator. Enter vector components to see the similarity score, angle, and step-by-step computation.
Key Takeaway
Use the free Cosine Similarity Calculator to calculate the cosine similarity between two vectors instantly. enter vector components to see the similarity score, angle, and step-by-step dot product computation. Get instant results with step-by-step explanations.
How to Use the Cosine Similarity Calculator
- Step 1: Enter the components of the first vector (comma-separated)
- Step 2: Enter the components of the second vector (comma-separated)
- Step 3: Click Calculate to see the cosine similarity and angle
- Step 4: Review the step-by-step dot product and magnitude computation
The Formula
Variable Definitions
- A · B: The dot product: sum of products of corresponding components (A₁B₁ + A₂B₂ + ...)
- |A|: The magnitude (length) of vector A: √(A₁² + A₂² + ...)
- |B|: The magnitude (length) of vector B: √(B₁² + B₂² + ...)
- θ: The angle between the two vectors in radians or degrees
- cos(θ): The cosine of the angle — equals the cosine similarity value
Cosine Similarity of A = [1, 2, 3] and B = [4, 5, 6]
Compute the cosine similarity and angle between two 3D vectors.
- Step 1: Dot product: A · B = (1×4) + (2×5) + (3×6) = 4 + 10 + 18 = 32.
- Step 2: Magnitude of A: |A| = √(1² + 2² + 3²) = √(1+4+9) = √14 ≈ 3.7417.
- Step 3: Magnitude of B: |B| = √(4² + 5² + 6²) = √(16+25+36) = √77 ≈ 8.7749.
- Step 4: Cosine similarity = 32 / (3.7417 × 8.7749) = 32 / 32.8329 ≈ 0.9746.
- Step 5: Angle: θ = arccos(0.9746) ≈ 12.93°. The vectors are nearly aligned.
Frequently Asked Questions
What does cosine similarity measure?
Cosine similarity measures the cosine of the angle between two vectors. It tells you how similar their directions are, regardless of their magnitudes. A value of 1 means identical direction, 0 means perpendicular, -1 means opposite.
Why is cosine similarity used in NLP?
In natural language processing, documents are represented as vectors. Cosine similarity measures how similar two documents are by comparing their vector directions, ignoring document length. It is the standard metric for text similarity.
What is the difference between cosine similarity and Euclidean distance?
Cosine similarity measures direction (angle), while Euclidean distance measures magnitude (straight-line distance). Two vectors can be close in cosine similarity but far in Euclidean distance if they have different magnitudes.
Can cosine similarity be negative?
Yes. A negative value means the vectors point in generally opposite directions. In practice, most document vectors have non-negative components, so cosine similarity ranges from 0 to 1.
How do I interpret a cosine similarity of 0.8?
A value of 0.8 means the vectors are quite similar — the angle between them is about 36.87°. In text similarity, this would indicate highly related documents.
What happens if one vector is all zeros?
If either vector is the zero vector, cosine similarity is undefined (division by zero). The calculator will report an error. In practice, zero vectors are filtered out before comparison.