WRITE · TRACE · UNDERSTAND

Cosine Retrieval

Vector length should not dominate relevance.

Your experiment

rank_vectors(query, documents, k) returns the top k document indices by descending cosine similarity, breaking ties by smaller index. Empty or zero vectors score 0. Dimensions match and k >= 0.

Python runs in your browser. Your draft stays on this device.

What to look for

Vector length should not dominate relevance. Normalize dot products by both norms, give zero vectors score zero, and keep stable indices for tied documents.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.