Zero Labs
Industry News / AI

Cosine Similarity Told Me ‘How Alike.’ It Never Told Me ‘How Much.

Source · Towards AI By Samishere

From the publisher’s feed, collected in the ZeroLabs newsroom.

What a coffee cup’s shadow taught me about vector projectionIt was late afternoon, and the sun was low enough that my coffee cup was casting a shadow halfway across the desk. I watched it for a while longer than I needed to, the way the shadow stretched out long and thin, nothing like the cup’s actual shape.That shadow isn’t really about the cup. It’s about the angle of the light. Move the sun lower; the shadow stretches. Move it directly overhead, and the shadow shrinks to almost nothing, even though the cup never changed size at all.It took me a few days to realize I’d just described vector projection without knowing it.An animation that shows the projection of the sun onto the desk.Vectors gave us a way to place things in space. The dot product gave us a number that blends direction and size together. Cosine similarity stripped out the size, leaving pure direction. Now, vector projection asks a different question: not just how aligned two vectors are, but how much of one actually lands in the direction of the other, leading to the discovery of a new mechanism that focuses on our purpose.In the previous article, we talked about cosine similarity and how it depends purely on angle; unlike the raw dot product, which also gets pulled around by vector size.But what if one of the vectors casts its shadow on the other vector? It would be easy to know how similar these two vectors are to each other, rather than knowing only that they are alike. That’s the vector projection, but how can we calculate it?Vector projectionGiven this example, about how similar these two movies are:Given the two vectors of the movies: [0.90, 0.88] and [0.95, 0.15], if we wanted to start calculating their cosine similarity first, using the following formula:cosine similarity = (A · B) / (‖A‖ ‖B‖)= (0.90 × 0.95 + 0.88 × 0.15) / (√(0.90 ² + 0.88 ²) · √(0.95 ² + 0.15 ²))= 0.987 / (1.259 × 0.962) = 0.815The cosine similarity comes out to 0.815, which corresponds to an angle of about 35.4° between the two vectors. So they’re fairly similar, though not nearly identical.Now we want to know how much one vector actually contributes to the other, using vector projection; but before that, let’s look at what the formula looks like.First, we have the scalar projection, which shows how much of A extends in B’s direction, meaning the line that crosses the vector B; meanwhile, the vector projection gives the whole shadow that was created when vector A was projected onto vector B. So now we know the formula; it's time to apply it:proj_B(A) = 0.987 / 0.925 × [0.95, 0.15]= 1.067 × [0.95, 0.15]≈ [1.014, 0.160]If projection is just “how much of A lands in B’s direction,” why isn’t the answer simply a number, the way distance was?While we know that the shadow doesn’t have just a length but also a location in space, so when I said “how far A’s shadow falls onto B,” I wasn’t just asking for a measurement; I was asking where that shadow actually sits in space. And “where, in a specific direction” is exactly what a vector is for.Look at the formula again: (A · B) / ‖B‖² is just a plain number, a scaling factor, nothing more. The moment we multiply that number by vector B, we're not calculating a fresh value from scratch. We're stretching or shrinking B itself by that amount. And scaling a vector, no matter how much we shrink or grow it, still gives you a vector in the same direction as B, just longer or shorter.That’s the part I’d been missing throughout this whole series without realizing it: distance is one number. Direction is a relationship between numbers. A vector needs both to mean anything. Scalar projection only gives you the first half; vector projection is what happens when you hand that number its direction back.This isn’t just useful for comparing movies. The same mechanism, measuring how much one vector contributes in the direction of another, is one of the building blocks behind how attention works in modern AI models. I’m not there yet in this series, but it’s worth knowing that this “shadow” idea doesn’t stay small for long.So now I know how to measure not just whether two vectors point the same way, but how much of one actually carries over into the other’s direction. That’s further than distance, further than the dot product, further than cosine similarity ever took me alone.But everything so far has been about comparing vectors that already exist. There’s a question sitting right underneath all of this that I haven’t touched yet: where do these vectors even come from? How does a movie, a word, an image become a list of numbers in the first place? That’s next: embeddings.If you’ve followed this series from a single vector all the way to this shadow, thank you for sticking with the math. The next piece is where it stops being abstract and starts being how AI actually sees the world.Cosine Similarity Told Me ‘How Alike.’ It Never Told Me ‘How Much. was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.