Toy embeddings and cosine similarity
The synonym test scores 0 because the toy only knows exact words. Real embeddings would not.
An embedding turns text into a vector so that texts with similar meaning end up close together. With NumPy you can score a query against every stored vector with one matrix multiplication and take the top results, which is the heart of semantic search.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write cosine(a, b) that returns 0.0 when either vector is all zeros.
Write top_k(query, matrix, k) that returns the indices of the k best rows by dot product, with ties broken by lower index.
Write normalise_rows(matrix) that scales each row to length 1 and leaves all-zero rows as zeros.
Explain it without notes
What is an embedding and why is cosine similarity a good way to compare two of them?
Why normalise vectors, and how does that make search a single matrix multiplication?
What does a vector database add over a NumPy matrix?
When do cosine similarity, dot product and Euclidean distance rank results identically, and what happens when you change the embedding model?
Expected output
0.000 python list and loop basics
0.866 cook pasta: boil water first
0.000 a vector model in python
synonym test: 0.0