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.

Understanding Cosine Similarity

How do we measure how "similar" two things are, especially when they are represented as lists of numbers (vectors)? While we could measure the distance between their endpoints (Euclidean distance), Cosine Similarity offers a different perspective: it measures the angle between two vectors. This simple but powerful idea is fundamental to modern AI, from search engines to recommendation systems. This 3D lab lets you build an intuition for what that angle really means.

The Core Idea: It's All About Direction

Imagine two arrows starting from the same point (the origin). Cosine similarity doesn't care about the length of these arrows (their "magnitude"); it only cares about the direction they are pointing.

  • If the vectors point in the exact same direction, the angle between them is 0°, and their cosine similarity is +1. They are considered identical in orientation.
  • If the vectors are perpendicular (at a 90° angle), they are considered unrelated or "orthogonal," and their cosine similarity is 0.
  • If the vectors point in opposite directions (180°), they are considered diametrically opposed, and their cosine similarity is -1.

This focus on direction is why cosine similarity is so useful. For example, in text analysis, the documents "the cat sat on the mat" and "a cat was sitting on a mat" are different in length and exact wording, but their vector representations will point in very similar directions, resulting in a high cosine similarity score.

How It's Calculated

The similarity score is literally the cosine of the angle ($\theta$) between the two vectors, $\vec{A}$ and $\vec{B}$. The formula looks like this:

Similarity = cos($\theta$) = (A · B) / (||A|| * ||B||)

Let's break that down:

A · B (The Dot Product)

This measures how much the vectors "agree." You calculate it by multiplying the corresponding components of each vector and summing the results. (A.x * B.x) + (A.y * B.y) + ...

||A|| and ||B|| (The Magnitudes)

This is the length of each vector, calculated using the Pythagorean theorem. By dividing by the magnitudes, we "normalize" the vectors, effectively ignoring their lengths and focusing only on their direction.

Guided Experiments

Use the controls and presets to explore the concept.

  1. Explore the Presets: Click the "Parallel", "90°" (Orthogonal), and "Opposite" buttons. Watch how the vectors align in the 3D space and see how the Angle and Cosine Similarity metrics update to 0° (+1), 90° (0), and 180° (-1) respectively.
  2. The Effect of Magnitude: Set Vector A to (1, 1, 0) and Vector B to (2, 2, 0). They are perfectly parallel, so the similarity is +1. Now, change Vector B to (5, 5, 0). The length of Vector B changes dramatically, but its direction doesn't. Notice that the angle remains 0° and the cosine similarity stays at +1. This is the key takeaway: magnitude doesn't matter.
  3. Create "Almost Similar" Vectors: Start with Vector A as (1, 1, 0) and Vector B as (1, 1, 0). Now, slightly nudge one of Vector B's components, for example, change its Y value to 1.2. The vectors are no longer perfectly aligned. Observe that the angle is now small (but not zero) and the cosine similarity is high (but not exactly 1). This is what happens in real-world applications like document similarity.

Key Takeaways

  • It's a Measure of Direction, Not Size: Cosine similarity ignores the magnitude (or length) of the vectors and only considers their orientation in space.
  • The Range is [-1, 1]: A score of +1 means identical direction, 0 means they are unrelated (orthogonal), and -1 means they are exact opposites.
  • Foundation for NLP: This is the primary way we measure the similarity between words, sentences, and entire documents after they have been converted into vector embeddings (like Word2Vec or BERT embeddings).
  • Powers Recommendation Engines: It's used to find similar users or items. If your vector of movie ratings is similar to someone else's, the engine might recommend movies you haven't seen that they liked.
Cheat sheet

Cosine Similarity Metric

How do we measure how "similar" two things are, especially when they are represented as lists of numbers (vectors)? While we could measure the distance between their endpoints (Euclidean distance), Cosine Similarity offers a different perspective: it measures the angle between two vectors. This simple but powerful idea is fundamental to modern AI, from search engines to recommendation systems. This 3D lab lets you build an intuition for what that angle really means.

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