Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 16.8 — Embeddings and Similarity Search with NumPy
Python · Example 1 of 3

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.

01

Write cosine(a, b) that returns 0.0 when either vector is all zeros.

02

Write top_k(query, matrix, k) that returns the indices of the k best rows by dot product, with ties broken by lower index.

03

Write normalise_rows(matrix) that scales each row to length 1 and leaves all-zero rows as zeros.

Explain it without notes

01

What is an embedding and why is cosine similarity a good way to compare two of them?

02

Why normalise vectors, and how does that make search a single matrix multiplication?

03

What does a vector database add over a NumPy matrix?

04

When do cosine similarity, dot product and Euclidean distance rank results identically, and what happens when you change the embedding model?

Toy embeddings and cosine similarity
Sign in to run this example in your browser.

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