After walking through vectors in the first article, how they define similarity in space, and then discovering the dot product in the second article, we found that it points to direction instead of distance to decide if two vectors are similar or not. For a while, that seemed like enough.
But diving deeper into the dot product, I found that direction alone isn’t the full story. It’s actually still tangled up with something else: the size of the vectors. So does that mean the dot product isn’t useful? Not exactly; it’s doing the job of finding similarity, but it’s using coordinates that can let magnitude quietly take over and skew the result.
If you’re new here: this is part 3 of a series walking through the math behind AI, one concept at a time, starting from vectors, then the dot product, and now this. Each article builds on the last. Both part one and two of this series ran on Bootcamp publication.
We said earlier that direction is what the dot product is really pointing to. That leads to a real question: does direction alone determine how similar two vectors are? It’s focusing more on direction, so why are we still letting vector size affect the calculation? That’s where cosine similarity comes in.
Before going deeper into cosine similarity and its math, imagine we have two vectors. If we plot them, we can measure the angle between them directly, and that angle alone tells us how aligned they are, regardless of how long either vector is.
To define cosine similarity properly: it takes the dot product and divides it by the length of both vectors, which cancels out magnitude and leaves only the angle between them.
Where:
In the first plot, we have vector 1 = [2, 2] and vector 2 = [0, 2]. The angle between them is 45°, which gives us cos(45°) = **0.707**.
In the second, we have vector 1 = [2, 2] and vector 2 = [1, 1], with an angle of 0° between them — same direction, different length. That gives us cos(0°) = **1**.
And lastly, vector 1 = [0, 2] and vector 2 = [2, 0], a 90° angle between them, which gives us cos(90°) = **0**.
If we go deeper into these examples, we find that when two vectors are close and pointing in the same direction, the cosine is close to 1, so they’re similar to each other. And that’s how cosine similarity works: without needing to know the vectors’ size at all, we can know their similarity using the angle alone.
Here’s the part that makes this worth the extra math. Take vector 1 = [2, 2] and vector 2 = [1, 1]; cosine similarity gives us 1, same as above. Now watch what happens if vector 2 gets scaled up to [4, 4] instead, same direction, much bigger size. The raw dot product jumps from 4 all the way to 16. But the cosine similarity? Still exactly 1.
Size changed everything for the dot product and nothing for cosine similarity. That’s the whole point.
Now we see that using cosine similarity makes a real difference when it comes to classifying similarity between vectors, especially when the vectors being compared don’t share the same scale. That’s exactly what search engines, recommendation systems, and embedding comparisons rely on. It’s not the raw dot product they use under the hood; it’s this.
Here’s what surprised me: I expected adding more math to make things more complicated. Instead, cosine similarity works by taking something away; it strips out the size of both vectors and keeps only the angle between them. Removing information was what made the comparison clearer, not something lost. That felt backwards at first, but it’s exactly why this version, not the raw dot product, is the one real systems actually use.
So now we know how to measure pure direction, independent of size. But there’s a question this doesn’t answer yet: how much does one vector actually contribute in the direction of another? Cosine similarity tells you how alike two directions are; it doesn’t tell you how much one vector projects onto the other. That’s the next thing I want to understand, which is the vector projection.
If you’re following this series as I figure it out in real time, the next piece picks up exactly where this one left off. How Do You Measure Direction Without Size Getting in the Way? was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.