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)(docs @ query) / (||query|| * ||docs_i||)Constraint: Use np.argsort (descending).
Math
Asked at
import numpy as np
def topk_cosine(...):
pass
Premium problem
Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.
Already premium?