Vectors, Embeddings and Similarity
Turn every thing (a word, a song, a customer) into a list of numbers so that similar things land near each other. Then "how alike are these?" becomes geometry: the angle between two arrows, or the distance between two points.
What the symbols mean
- Angle
The angle between the two arrows; small angle means similar direction.
- Dot product
Multiply matching entries and add them up.
- Length of u
The square root of the sum of squares of u's entries.
- Length of v
The square root of the sum of squares of v's entries.
What the symbols mean
- Distance
How far apart the two points are: Pythagoras in n dimensions.
- Dimension
How many numbers are in each vector, often hundreds for learned embeddings.
Explanation
An embedding is simply a list of numbers that stands in for a thing (a word, a song, a photo, a customer), chosen so that similar things get similar lists. The name comes from geometry: we embed each thing as a point in a space with many dimensions, often a few hundred. Once things are points, "how alike are these two?" turns into a question you already know how to answer: how far apart are the points, or how small is the angle between their arrows? The cosine formula looks busy, but it's three pieces: the dot product (multiply matching entries and add), and the two lengths and , which we divide by so that only the direction counts. That's the whole idea.
Data as vectors. Some vectors are written by hand: a house is (size, bedrooms, age), a patient is (heart rate, temperature, blood pressure). Others are learned: a network adjusts each word's vector until dot products predict which words show up together. In a learned embedding no single coordinate means much on its own. What matters is the geometry: "car" and "truck" end up pointing in nearly the same direction, "car" and "banana" don't.
Why the dot product measures alignment. Put and tail to tail. The third side of the triangle is , and the law of cosines says
Now expand the left side with dot products instead: . The squared lengths match on both sides, so what's left must match too: . Divide by the lengths and you have the formula above. So cosine similarity runs from (same direction) through (perpendicular, unrelated) to (opposite). This is the same dot product as in The Dot Product and Inner Products; here the vectors just have more entries.
Cosine or distance? Cosine ignores length; distance doesn't. Length often tracks something incidental, like how long a document is or how popular a song is. A document that says the same thing twice has a vector twice as long, pointing the same way. When every vector has length 1, the two measures agree. Set in the law of cosines and you get
so a bigger cosine always means a smaller distance. That's why search systems usually scale every embedding to length 1 first: then "nearest" and "most similar" are the same ranking.
Nearest neighbors. The simplest thing you can do with embeddings is k-nearest neighbors (k-NN): to label a new point, find the stored points most similar to it and let them vote. There's no training at all; the embedding does the work. The cost is the catch: comparing one query with stored vectors of dimension takes multiplications, which is why large systems use approximate indexes that skip most of the comparisons.
Correlation is a cosine. Here's a connection you may not expect. Take two columns of data, subtract each column's mean, and compute the cosine between the centered columns. You get exactly the correlation coefficient from Correlation. That's why is always between and .
A common mistake. It's tempting to think the closest point and the most similar point are always the same. They aren't when lengths differ. Take the query and two stored items, and . is much closer (distance , versus about for ), but points in exactly the same direction (cosine , versus about for ). Neither answer is wrong: decide whether length means something in your data. A related trap: features on wildly different scales (income in dollars next to age in years) let the big one swamp every distance, so standardize them first.
There's an animation further down that swings one vector around another so you can watch both measures change.
See it
Worked examples
A toy embedding gives "car" the vector and "truck" the vector . Find their cosine similarity and the angle between them.
- 1
Start with the dot product: multiply matching entries. That's .
A news app embeds articles in 2D. A new article has . Stored articles: sports, news, sports, news. Classify by 3-nearest neighbors using cosine similarity.
- 1
Every cosine divides by , so compute it once: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Find the cosine similarity of and , to 4 decimal places.
Find the Euclidean distance between the embeddings and .
For a nonzero vector , what are the cosine similarities of with and of with ?
A photo-search system stores every image embedding scaled to length 1. Two images have cosine similarity . How far apart are their embeddings?
A fruit sorter measures (weight in 100 g, redness score) and stores labeled examples: apple, apple, pear, pear, pear. A new fruit measures . Using Euclidean distance, what do 3-NN and 5-NN predict?
Center and by subtracting each one's mean, then find the cosine similarity of the centered vectors, to 4 decimal places. (This is the correlation coefficient .)
Find the angle, in degrees, between and .
The hardest one here. Get it, and you've got this lesson.
Two embeddings have lengths and , and they are apart. A search system scales both to length 1 before comparing. How far apart are the scaled vectors? Give 4 decimal places.
Where it's used
When you search a large photo library for "dog on a beach", the system embeds your words and every photo into the same space and returns the photos with the highest cosine similarity to your query. Chat assistants that answer from your documents do the same: they embed each paragraph once, embed the question, and pull the nearest paragraphs to read before answering. Phone face unlock stores one embedding of your face and accepts a new photo when the distance to it falls under a threshold.