IIT Madras Zanzibar · Teaching

← Z3003 Data Mining

Tutorial 5: MinHash & LSH

datasketch · MinHash · MinHashLSH  ·  Submit as dm05t[RR].py

New library: datasketch. Install before starting: 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.

  1. 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())
  2. Print set A, set B, their intersection, and their union
  3. Compute and print the exact Jaccard similarity: len(A & B) / len(A | B)
Set A: {'about', 'data', 'datasets', 'finding', 'in', 'is', 'large', 'mining', 'patterns'} Set B: {'about', 'data', 'finding', 'in', 'insights', 'is', 'large', 'science', 'sets'} Intersection: {'about', 'data', 'finding', 'in', 'is', 'large'} Union: 12 elements Exact Jaccard: 0.5000

Task 2 Create MinHash Signatures

  1. Import: from datasketch import MinHash
  2. Create two MinHash objects with num_perm=128 (128 hash functions):
    m1 = MinHash(num_perm=128), m2 = MinHash(num_perm=128)
  3. Update each with the elements of set A and set B respectively. Elements must be encoded: m1.update(word.encode('utf8'))
  4. Compute and print the MinHash-estimated Jaccard similarity: m1.jaccard(m2)
  5. Compute the absolute error between the exact and estimated Jaccard values
MinHash estimated Jaccard: ~0.50 (varies slightly with hash functions) Absolute error vs exact: ~0.00–0.05
MinHash is an approximation. The error decreases as num_perm increases: try 32, 64, 128, 256 and observe the trend.

Task 3 Build a Corpus and Detect Near-Duplicates with LSH

  1. Import: from datasketch import MinHashLSH
  2. 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"}
  3. Create a MinHashLSH with threshold=0.5, num_perm=64:
    lsh = MinHashLSH(threshold=0.5, num_perm=64)
  4. For each document, create a MinHash and insert it into the LSH index: lsh.insert(key, minhash)
  5. For each document, query the LSH for its neighbours: result = lsh.query(minhash). Print the document and its neighbours (excluding itself)
Near-duplicates (threshold >= 0.5): d1 -> ['d2', 'd4'] d2 -> ['d1', 'd4'] d3 -> ['d5'] d4 -> ['d1', 'd2'] d5 -> ['d3'] d6 -> []
Documents d1, d2, d4 form a near-duplicate cluster. d3 and d5 form another. d6 is unique. LSH found these without comparing all 15 pairs.

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.

threshold=0.3: [N] near-duplicate pairs found threshold=0.5: [N] near-duplicate pairs found threshold=0.7: [N] near-duplicate pairs found

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.

A character 3-shingle of "cat" includes: "cat", "at ", "t s", etc. Use a set comprehension: {doc[i:i+3] for i in range(len(doc)-2)}.