Tutorial 5: MinHash & LSH
datasketch · MinHash · MinHashLSH · Submit as dm05t[RR].py
pip install datasketch. No other dependencies required.
Submission
Name your file dm05t[RR].py where RR = last two digits of your roll number. All tasks in one file with # Task N comments. Submit by the deadline announced in class.
Background
Jaccard similarity between two sets A and B is |A ∩ B| / |A ∪ B|. Finding all near-duplicate documents by comparing every pair is O(n²): infeasible at scale. MinHash creates a compact signature for a set such that the probability that two signatures agree on any one hash equals the Jaccard similarity. LSH (Locality-Sensitive Hashing) uses MinHash signatures to bucket similar items together: only items in the same bucket need to be compared, making near-duplicate detection sub-linear.
This tutorial uses the datasketch library, which implements both efficiently.
Tasks
Task 1 Compute Jaccard Similarity by Hand
Before using MinHash, compute the exact Jaccard similarity so you have a reference to compare the approximation against.
- Define two sets of words (shingles):
A = set("data mining is about finding patterns in large datasets".split())B = set("data science is about finding insights in large data sets".split()) - Print set A, set B, their intersection, and their union
- Compute and print the exact Jaccard similarity:
len(A & B) / len(A | B)
Task 2 Create MinHash Signatures
- Import:
from datasketch import MinHash - Create two MinHash objects with
num_perm=128(128 hash functions):m1 = MinHash(num_perm=128),m2 = MinHash(num_perm=128) - Update each with the elements of set A and set B respectively. Elements must be encoded:
m1.update(word.encode('utf8')) - Compute and print the MinHash-estimated Jaccard similarity:
m1.jaccard(m2) - Compute the absolute error between the exact and estimated Jaccard values
num_perm increases: try 32, 64, 128, 256 and observe the trend.Task 3 Build a Corpus and Detect Near-Duplicates with LSH
- Import:
from datasketch import MinHashLSH - Define a corpus of 6 short documents:
docs = {"d1": "cat sat on the mat", "d2": "cat sat on the hat", "d3": "dog ran in the park", "d4": "cat sat on a hat", "d5": "dog ran through the park", "d6": "fish swam in the sea"} - Create a MinHashLSH with
threshold=0.5, num_perm=64:lsh = MinHashLSH(threshold=0.5, num_perm=64) - For each document, create a MinHash and insert it into the LSH index:
lsh.insert(key, minhash) - For each document, query the LSH for its neighbours:
result = lsh.query(minhash). Print the document and its neighbours (excluding itself)
Task 4 Effect of Threshold on LSH Results
Rebuild the LSH index three times with thresholds 0.3, 0.5, and 0.7. For each threshold, print how many near-duplicate pairs are found across all queries.
Write a comment explaining why lowering the threshold increases the number of pairs.
Task 5 Extension: Shingle-Based Similarity
Instead of word-level tokens, use character 3-shingles (consecutive character trigrams) for documents d1 and d2. Recompute the exact Jaccard and the MinHash-estimated Jaccard. Is character shingle similarity higher or lower than word similarity for these two documents? Write your observation as a comment.
{doc[i:i+3] for i in range(len(doc)-2)}.