Embeddings and Cosine Similarity, Explained in Python
Computers compare numbers easily and meanings not at all. An embedding bridges the gap: it represents a piece of text (or an image, or a product) as a list of numbers, a vector, arranged so that things with similar meanings get similar vectors. "Puppy" and "dog" end up close together; "puppy" and "invoice" far apart.
Where the numbers come from
Embeddings are produced by a trained model. During training the model adjusts the vectors so that items appearing in similar contexts drift together. Real embeddings have hundreds or thousands of dimensions, and an individual dimension usually has no simple human meaning. To make the idea visible, the example uses hand-made three-dimensional vectors where each number loosely means "animal-ness", "food-ness" and "tech-ness".
Cosine similarity
To compare two vectors, the most common measure is cosine similarity: the cosine of the angle between them.
- It is computed as the dot product (multiply matching numbers and add them up) divided by the product of the two vectors' lengths.
- The result ranges from -1 to 1. Close to 1 means the vectors point the same way (similar meaning); around 0 means unrelated.
- Because it measures direction, not length, a long document and a short one about the same topic can still score as similar.
Expected output
cat vs kitten: 0.996 cat vs pizza: 0.165 cat vs laptop: 0.061 cat 0.999 kitten 0.999 dog 0.992 pizza 0.188 laptop 0.029
The cosine function is three lines of plain Python. The ranking at the end is a tiny semantic search: score every item against a query vector and sort. Notice that "kitten" ranks right after "cat" even though the two words share almost no letters. Matching by meaning instead of spelling is the whole point of embeddings.
What embeddings are used for
- Semantic search: find documents about the same topic as a question, even when they use different words.
- Retrieval for chat assistants: look up the most relevant passages and include them in the prompt, so answers can draw on your own documents. This pattern is often called retrieval-augmented generation (RAG).
- Recommendations: suggest products whose vectors are close to ones a customer liked.
- Clustering and deduplication: group similar support tickets or spot near-duplicate pages.
Searching millions of vectors one by one is slow, so production systems use specialized indexes, often in a vector database, that find approximate nearest neighbors quickly. It is the k-NN idea from the previous chapters at scale.
Limits
Embeddings inherit the patterns and biases of the data their model was trained on. Similar vectors mean "used in similar ways in the training text", which is not always the same as "means the same thing" or "is true". Always compare vectors produced by the same embedding model; vectors from different models are not compatible.
Summary
- An embedding is a vector that represents meaning; similar items get nearby vectors.
- Cosine similarity compares direction: near 1 is similar, near 0 unrelated.
- Semantic search, retrieval and recommendations rank items by similarity.
Quiz: Embeddings and similarity