Exact nearest-neighbour search costs per query. Locality sensitive hashing (LSH) instead hashes points so that similar points tend to share a bucket, and only the bucket is searched. For cosine similarity, a random hyperplane with normal assigns the bit .
The hyperplanes are given as an array R of shape (T, B, D): hash tables, each with hyperplanes in dimensions. In table a point's code is the integer .
Implement:
lsh_codes(X, R): integer array of shape (T, N) with the code of every row of X (shape (N, D)) in every table.query_lsh(X, R, q, k): the candidates are all indices whose code equals the query's code in at least one table. Rank the candidates by exact cosine similarity to q, largest first, breaking ties by the smaller index, and return (top, n_candidates) where top is a list of at most k Python ints and n_candidates is the number of distinct candidates.recall_at_k(X, R, Q, k): for each query row of Q, compare the query_lsh result with the exact top- by cosine similarity over all of X (same tie rule) and return the mean over queries of as a float.No row of X or Q is the zero vector.