Cosine Similarity

Cosine

Part of: Embeddings & Semantic Search

Here is the problem with dot product alone: a vector that is just longer scores higher even when it points the same way. A 2,000-word essay about cars and a quick "I love cars" tweet should rank as equally car-ish. Raw dot product punishes the tweet for being short. The fix is to throw away length and keep only direction . That is cosine similarity. What it is Cosine similarity is the cosine of the angle between two vectors. Two arrows pointing the same way have a 0-degree angle, and cos(0) = 1. Two arrows at right angles have a 90-degree angle, and cos(90) = 0. The score tells you alignment, and alignment is exactly what "similar meaning" looks like on the map. How it works You already wrote dot and magnitude last lesson. Cosine glues them together: Dividing the dot product by both magnitudes cancels out the length of each vector. What survives is purely the angle between them. Step by step: compute the raw alignment with the dot product, then divide away how long each arrow happens to be, and you are left with the cosine of the angle. Reading the score is simple: - 1.0 , same direction. Basically synonyms. - 0.0 , perpendicular. Unrelated. - -1.0 , opposite. Rare with real text e

Challenge: Closest by Angle