TorchedUp
ProblemsPremium
TorchedUp
Cosine Top-K RetrievalEasy
ProblemsPremium

Cosine Top-K Retrieval

Given a query embedding and a matrix of document embeddings, return the indices of the top-k documents by cosine similarity.

Signature: def topk_cosine(query: np.ndarray, docs: np.ndarray, k: int) -> list

  • query shape (d,), docs shape (n, d)
  • Cosine sim: (docs @ query) / (||query|| * ||docs_i||)
  • Return a Python list of indices, most-similar first.

Constraint: Use np.argsort (descending).

Math

Asked at

Python (numpy)0/3 runs today

Test Results

○axis-aligned
○k=1
○negative directions🔒 Premium
Advertisement