Distances bunch together as dimensions grow
Uniform random points have no structure, so this exaggerates the effect for real embeddings. It shows the direction of the problem, not the exact numbers you will see on real data.
Sorting and tree structures that make ordinary database lookups fast stop helping when vectors have hundreds of numbers. Vector indexes therefore trade a little accuracy for a lot of speed, and the accuracy is measured as recall against the exact answer.
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 recall_at_k(found, truth) that returns the fraction of truth present in found. Print it with 3 decimals for found [3, 1, 7, 9] against truth [1, 2, 3, 4], for an empty found list, and for the same four ids in reverse order.
Three queries have (found, truth) pairs ([1, 2, 3], [1, 2, 3]), ([4, 5, 6], [4, 9, 6]) and ([7, 8, 9], [1, 2, 3]). Print each query's recall and then the average and the worst, to 3 decimals. What does the average hide?
Explain it without notes
Why can a database find one number among millions with about twenty steps, but not the nearest vector among millions?
What does the distance table in the first example show, and why does it hurt indexing?
Define recall@k and compute it for found = [3, 1, 7, 9] against truth = [1, 2, 3, 4].
Why must the ground truth for recall come from exact search over the whole collection?
A teammate says the index has recall 0.97, so it is fine. What would you check before agreeing?
Expected output
300 random points; distance from one query to its nearest and its farthest point
2 dims: nearest 0.024, farthest 1.093, nearest/farthest 0.022
16 dims: nearest 0.932, farthest 2.248, nearest/farthest 0.415
128 dims: nearest 4.011, farthest 5.274, nearest/farthest 0.760