Neural Mastery
← Back to Practice

Cosine Similarity From Scratch

Difficulty: Easy · Concept: Word Embeddings — The Distributional Hypothesis

cos(θ)=abab\cos(\theta) = \frac{a \cdot b}{\|a\| \, \|b\|}

Cosine similarity measures how aligned two vectors are in direction, ignoring magnitude entirely — 1 means pointing the same way, 0 means orthogonal (unrelated), -1 means pointing opposite ways. This is the standard way to compare embeddings — word embeddings, sentence embeddings, and every vector database similarity search on this site all default to it, precisely because magnitude in embedding space usually reflects something irrelevant (like word frequency) while direction reflects meaning.

Your task: implement cosine_similarity(a, b) for two equal-length lists of numbers, without NumPy.

Implement it yourself
assert abs(cosine_similarity([1, 0], [0, 1]) - 0.0) < 1e-9 assert abs(cosine_similarity([1, 1], [1, 1]) - 1.0) < 1e-9 assert abs(cosine_similarity([1, 0], [-1, 0]) - (-1.0)) < 1e-9 assert abs(cosine_similarity([3, 4], [6, 8]) - 1.0) < 1e-9 assert abs(cosine_similarity([1, 2, 3], [4, 5, 6]) - 32 / (14 ** 0.5 * 77 ** 0.5)) < 1e-9

Next: Pairwise Cosine Similarity Matrix (a harder variant), or skip ahead to TF-IDF Weight From Scratch

Last updated Sep 5, 2026Edit this pageReport an issue