Cosine Similarity Calculator

Compare two vectors with cosine similarity, dot product, and Euclidean distance, the standard metrics for comparing embeddings.

How cosine similarity is calculated

cosine(A, B) = (A · B) / (‖A‖ × ‖B‖), the dot product of the two vectors divided by the product of their magnitudes. The dot product alone grows with vector length, dividing by both norms cancels that out and leaves only the angle between the two directions, which is the part that usually matters for comparing meaning rather than scale.

Worked example

Vectors [1, 2, 3] and [4, 5, 6]: dot product is 1×4 + 2×5 + 3×6 = 32. ‖A‖ = √14 ≈ 3.7417, ‖B‖ = √77 ≈ 8.7750. Cosine similarity is 32 / (3.7417 × 8.7750) ≈ 0.9746, a high similarity since both vectors point in a fairly close direction even though B is nearly twice as long as A. Euclidean distance between the same two points is √27 ≈ 5.1962, a completely different number telling a different story: how far apart the points sit in space, not how aligned their directions are.

Limitations of this calculator

Both vectors must be the same length, and the calculator does not handle sparse or extremely high-dimensional vectors gracefully in the input box, it is meant for quick sanity checks, not for comparing real embedding vectors with hundreds or thousands of dimensions. For that, compute the comparison directly in code with NumPy or your vector database's built-in similarity function.

How to calculate cosine similarity in Python

import numpy as np def cosine_similarity(a, b): a, b = np.array(a), np.array(b) return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) a = [1, 2, 3] b = [4, 5, 6] print(round(cosine_similarity(a, b), 4)) # 0.9746

This tool is for quick manual checks on small vectors. Use a proper vector library or database for production embedding comparisons at scale.

Related Tools