Registry Synced

Locality-Sensitive Hashing

147 words
1 min read

Reading compass

Now · 5.1 Intuition

Locality-Sensitive Hashing

5.1 Intuition

LSH hashes points so that similar points map to the same bucket with high probability, while dissimilar points map to different buckets with high probability.

5.2 Random Hyperplane LSH for Cosine Similarity

python
import numpy as np
class RandomHyperplaneLSH:
    def __init__(self, n_hyperplanes=10):
        self.n_hyperplanes = n_hyperplanes
        self.planes = None
    def fit(self, X):
        d = X.shape[1]
        self.planes = np.random.randn(self.n_hyperplanes, d)
    def hash_vector(self, x):
        return tuple((x @ self.planes.T > 0).astype(int))
    def query(self, x, database):
        h = self.hash_vector(x)
        candidates = [i for i, db_h in enumerate(self.hashes)
                      if db_h == h]
        return candidates

5.3 LSH Families

A family H\mathcal{H} of functions is (r1,r2,p1,p2)(r_1, r_2, p_1, p_2)-sensitive if:
  • Pr⁡[h(x)=h(y)]≥p1\Pr[h(x) = h(y)] \geq p_1 when ∣∣x−y∣∣≤r1||x-y|| \leq r_1
  • Pr⁡[h(x)=h(y)]≤p2\Pr[h(x) = h(y)] \leq p_2 when ∣∣x−y∣∣≥r2||x-y|| \geq r_2 Common families: random hyperplanes (cosine), minhash (Jaccard), p-stable distributions (Euclidean). Join Discord PreviousJL ProofNextMinHash, SimHash, Bloom
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.