No history yet

cosine similarity

Welcome! I am looking forward to exploring cosine similarity with you today. To make sure we tailor this session to your specific needs, what is your background in machine learning or data science, and what specific project or goal led you to ask about cosine similarity?

I am a beginner looking to understand the core concept for a text recommendation system.

Your path
Mathematical Foundations
Applications and Use Cases
Practical Implementation

It's great to have you here! Building a text recommendation system is a classic, highly rewarding machine learning project, and cosine similarity is one of the most powerful and intuitive tools you can use for it. Let's start with the big picture: what exactly is this metric?

What is Cosine Similarity?

At its core, cosine similarity is a metric used to measure how similar two items are, regardless of their size. In machine learning, we represent items (like documents, products, or user profiles) as in a multi-dimensional space. Cosine similarity measures the direction or orientation of these vectors rather than their length.
Imagine you are comparing two online articles about hiking. Article A is a short blog post of 200 words, and Article B is a massive, comprehensive guide of 5,000 words. Because they cover the same topic, they use very similar vocabulary, but Article B uses those words much more frequently simply because it is longer. If you measured the literal distance between them, they would seem incredibly far apart because of their length difference. Cosine similarity solves this by ignoring the length (the size of the vector) and focusing entirely on the angle between the two vectors. If the arrows point in nearly the same direction, the angle is small, meaning the content is highly similar!

Show me a diagram of the vector angles

You've got it! Visualizing how these angles play out in space makes the concept incredibly intuitive. Let's look at exactly how we map those two hiking articles as vectors to see why the angle is what matters.

A 2D coordinate system plotting two collinear blue vectors at a 30-degree angle and one orthogonal red vector at a 90-degree angle.

As you'll see in the diagram once it finishes generating, even though the long article's arrow stretches far out into space, it lies along the exact same path as the short article's arrow. Because the angle between them is zero, their is at its absolute maximum. This highlights the core strength of this metric: it gives us a clean way to measure content similarity without getting tripped up by how wordy an author is.
Now that we have a solid visual grasp of the geometry, let's look at the mathematical range of these values so you know what to expect when your recommendation algorithm outputs its scores.