Home / Linear Algebra

Cosine Similarity

Interactive 3D visualization of vector relationships, angles, and similarity metrics.

Vectors

X
Y
Z
X
Y
Z

Presets

3D Space

Drag Tips

Metrics

Cosine Similarity
0.00 cos(θ)
-1 0 1
Angle (θ)
90° Degrees
1.57 Radians
Dot Product 0.0
A · B

Formula:

similarity = (A · B) / (||A|| ||B||)

Measures the cosine of the angle between two vectors.

Overview

Cosine similarity compares the direction of two nonzero vectors using their normalized dot product. It is useful when orientation matters more than magnitude, such as comparing document embeddings; it can discard useful information when vector length itself matters.

Definition

cos(a, b) = (a · b) / (||a|| ||b||)

The result is 1 when the vectors point in the same direction, 0 when they are perpendicular, and −1 when they point in opposite directions. In many text and embedding systems vectors are nonnegative or normalized, so observed values may fall in a narrower range.

Worked example

Let (a=[3,2,0]) and (b=[30,20,0]). Since (b=10a), they point in exactly the same direction and their cosine similarity is 1. For (c=[0,1,5]), the dot product with (a) is 2; dividing by the vector lengths gives about 0.11. The latter shares little direction with (a).

This differs from Euclidean distance: (a) and (b) are far apart in length even though their directions match. Cosine is therefore useful for comparing document vectors when document length should not dominate. If magnitude itself carries meaning, cosine can discard useful information.

Explore the vectors

Choose Parallel, 90°, or Opposite in the visualizer and watch the angle and similarity change. Then scale one vector without changing its direction: the cosine remains constant. The demonstration makes the geometry visible; it does not imply that a high embedding similarity proves two texts mean the same thing.

Where it helps and where it fails

  • Search and retrieval: rank document embeddings by similarity to a query, then evaluate whether the retrieved passages answer the query.
  • Recommendations: compare user and item vectors when relative feature direction is meaningful.
  • Clustering: cosine distance (1 - similarity) can group vectors by orientation.
  • Zero vectors: the formula is undefined if either vector has length zero; define a handling rule or use a library implementation that handles this case.
  • Meaning: similarity depends on how vectors were built and the dataset. A score is a ranking signal, not a universal semantic probability.
from sklearn.metrics.pairwise import cosine_similarity

scores = cosine_similarity([[3, 2, 0]], [[30, 20, 0], [0, 1, 5]])
print(scores)  # [[1.         0.108...]]

Sources and next steps

Predict, then reveal

About to run: double X to 2. Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “Definition”?

  2. What does this module say about “Worked example”?

  3. What does this module say about “Explore the vectors”?

Cheat sheet

Cosine Similarity Metric

Measure similarity by angle instead of distance. Drag two vectors apart and watch the cosine similarity score respond in real time.

MACHINE LEARNING · vizlearn.in/machine_learning/cosine_similarity.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.