Locality-Sensitive Hashing
147 words
1 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# 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 ## 5.3 LSH Families A family $\mathcal{H}$ of functio...

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
pythonimport 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 of functions is (r1,r2,p1,p2)-sensitive if:
- Pr[h(x)=h(y)]≥p1 when ∣∣x−y∣∣≤r1
- Pr[h(x)=h(y)]≤p2 when ∣∣x−y∣∣≥r2 Common families: random hyperplanes (cosine), minhash (Jaccard), p-stable distributions (Euclidean). Join Discord PreviousJL ProofNextMinHash, SimHash, Bloom