SlideShare a Scribd company logo
Mining of Massive Datasets
Jure Leskovec, Anand Rajaraman, Jeff Ullman
Stanford University
http://www.mmds.org
Note to other teachers and users of these slides: We would be delighted if you found this our
material useful in giving your own lectures. Feel free to use these slides verbatim, or to modify
them to fit your own needs. If you make use of a significant portion of these slides in your own
lecture, please include this message, or a link to our web site: http://www.mmds.org
High dim.
data
Locality
sensitive
hashing
Clustering
Dimensional
ity
reduction
Graph
data
PageRank,
SimRank
Network
Analysis
Spam
Detection
Infinite
data
Filtering
data
streams
Web
advertising
Queries on
streams
Machine
learning
SVM
Decision
Trees
Perceptron,
kNN
Apps
Recommen
der systems
Association
Rules
Duplicate
document
detection
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 2
3J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
[Hays and Efros, SIGGRAPH 2007]
4J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
[Hays and Efros, SIGGRAPH 2007]
10 nearest neighbors from a collection of 20,000 images
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 5
[Hays and Efros, SIGGRAPH 2007]
10 nearest neighbors from a collection of 2 million images
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 6
[Hays and Efros, SIGGRAPH 2007]
 Many problems can be expressed as
finding “similar” sets:
 Find near-neighbors in high-dimensional space
 Examples:
 Pages with similar words
 For duplicate detection, classification by topic
 Customers who purchased similar products
 Products with similar customer sets
 Images with similar features
 Users who visited similar websites
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 7
 Given: High dimensional data points 𝒙 𝟏, 𝒙 𝟐, …
 For example: Image is a long vector of pixel colors
1 2 1
0 2 1
0 1 0
→ [1 2 1 0 2 1 0 1 0]
 And some distance function 𝒅(𝒙 𝟏, 𝒙 𝟐)
 Which quantifies the “distance” between 𝒙 𝟏 and 𝒙 𝟐
 Goal: Find all pairs of data points (𝒙𝒊, 𝒙𝒋) that are
within some distance threshold 𝒅 𝒙𝒊, 𝒙𝒋 ≤ 𝒔
 Note: Naïve solution would take 𝑶 𝑵 𝟐

where 𝑵 is the number of data points
 MAGIC: This can be done in 𝑶 𝑵 !! How?
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 8
 Last time: Finding frequent pairs
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 9
Items1…N
Items 1…N
Count of pair {i,j}
in the data
Naïve solution:
Single pass but requires
space quadratic in the
number of items
Items1…K
Items 1…K
Count of pair {i,j}
in the data
A-Priori:
First pass: Find frequent singletons
For a pair to be a frequent pair
candidate, its singletons have to be
frequent!
Second pass:
Count only candidate pairs!
N … number of distinct items
K … number of items with support  s
 Last time: Finding frequent pairs
 Further improvement: PCY
 Pass 1:
 Count exact frequency of each item:
 Take pairs of items {i,j}, hash them into B buckets and
count of the number of pairs that hashed to each bucket:
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 10
Items 1…N
Basket 1: {1,2,3}
Pairs: {1,2} {1,3} {2,3}
Buckets 1…B
2 1
 Last time: Finding frequent pairs
 Further improvement: PCY
 Pass 1:
 Count exact frequency of each item:
 Take pairs of items {i,j}, hash them into B buckets and
count of the number of pairs that hashed to each bucket:
 Pass 2:
 For a pair {i,j} to be a candidate for
a frequent pair, its singletons {i}, {j}
have to be frequent and the pair
has to hash to a frequent bucket!
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 11
Items 1…N
Basket 1: {1,2,3}
Pairs: {1,2} {1,3} {2,3}
Basket 2: {1,2,4}
Pairs: {1,2} {1,4} {2,4}
Buckets 1…B
3 1 2
 Last time: Finding frequent pairs
 Further improvement: PCY
 Pass 1:
 Count exact frequency of each item:
 Take pairs of items {i,j}, hash them into B buckets and
count of the number of pairs that hashed to each bucket:
 Pass 2:
 For a pair {i,j} to be a candidate for
a frequent pair, its singletons have
to be frequent and its has to hash
to a frequent bucket!
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 12
Items 1…N
Basket 1: {1,2,3}
Pairs: {1,2} {1,3} {2,3}
Basket 2: {1,2,4}
Pairs: {1,2} {1,4} {2,4}
Buckets 1…B
3 1 2
Previous lecture: A-Priori
Main idea: Candidates
Instead of keeping a count of each pair, only keep a count
of candidate pairs!
Today’s lecture: Find pairs of similar docs
Main idea: Candidates
-- Pass 1:Take documents and hash them to buckets such that
documents that are similar hash to the same bucket
-- Pass 2: Only compare documents that are candidates
(i.e., they hashed to a same bucket)
Benefits: Instead of O(N2) comparisons, we need O(N)
comparisons to find similar documents
Ch03 Mining Massive Data Sets  stanford
 Goal: Find near-neighbors in high-dim. space
 We formally define “near neighbors” as
points that are a “small distance” apart
 For each application, we first need to define
what “distance” means
 Today: Jaccard distance/similarity
 The Jaccard similarity of two sets is the size of their
intersection divided by the size of their union:
sim(C1, C2) = |C1C2|/|C1C2|
 Jaccard distance: d(C1, C2) = 1 - |C1C2|/|C1C2|
14J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
3 in intersection
8 in union
Jaccard similarity= 3/8
Jaccard distance = 5/8
 Goal: Given a large number (𝑵 in the millions or
billions) of documents, find “near duplicate” pairs
 Applications:
 Mirror websites, or approximate mirrors
 Don’t want to show both in search results
 Similar news articles at many news sites
 Cluster articles by “same story”
 Problems:
 Many small pieces of one document can appear
out of order in another
 Too many documents to compare all pairs
 Documents are so large or so many that they cannot
fit in main memory
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 15
1. Shingling: Convert documents to sets
2. Min-Hashing: Convert large sets to short
signatures, while preserving similarity
3. Locality-Sensitive Hashing: Focus on
pairs of signatures likely to be from
similar documents
 Candidate pairs!
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 16
17
Docu-
ment
The set
of strings
of length k
that appear
in the doc-
ument
Signatures:
short integer
vectors that
represent the
sets, and
reflect their
similarity
Locality-
Sensitive
Hashing
Candidate
pairs:
those pairs
of signatures
that we need
to test for
similarity
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
Step 1: Shingling: Convert documents to sets
Docu-
ment
The set
of strings
of length k
that appear
in the doc-
ument
 Step 1: Shingling: Convert documents to sets
 Simple approaches:
 Document = set of words appearing in document
 Document = set of “important” words
 Don’t work well for this application. Why?
 Need to account for ordering of words!
 A different way: Shingles!
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 19
 A k-shingle (or k-gram) for a document is a
sequence of k tokens that appears in the doc
 Tokens can be characters, words or something
else, depending on the application
 Assume tokens = characters for examples
 Example: k=2; document D1 = abcab
Set of 2-shingles: S(D1) = {ab, bc, ca}
 Option: Shingles as a bag (multiset), count ab
twice: S’(D1) = {ab, bc, ca, ab}
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 20
 To compress long shingles, we can hash them
to (say) 4 bytes
 Represent a document by the set of hash
values of its k-shingles
 Idea: Two documents could (rarely) appear to have
shingles in common, when in fact only the hash-
values were shared
 Example: k=2; document D1= abcab
Set of 2-shingles: S(D1) = {ab, bc, ca}
Hash the singles: h(D1) = {1, 5, 7}
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 21
 Document D1 is a set of its k-shingles C1=S(D1)
 Equivalently, each document is a
0/1 vector in the space of k-shingles
 Each unique shingle is a dimension
 Vectors are very sparse
 A natural similarity measure is the
Jaccard similarity:
sim(D1, D2) = |C1C2|/|C1C2|
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 22
 Documents that have lots of shingles in
common have similar text, even if the text
appears in different order
 Caveat: You must pick k large enough, or most
documents will have most shingles
 k = 5 is OK for short documents
 k = 10 is better for long documents
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 23
 Suppose we need to find near-duplicate
documents among 𝑵 = 𝟏 million documents
 Naïvely, we would have to compute pairwise
Jaccard similarities for every pair of docs
 𝑵(𝑵 − 𝟏)/𝟐 ≈ 5*1011 comparisons
 At 105 secs/day and 106 comparisons/sec,
it would take 5 days
 For 𝑵 = 𝟏𝟎 million, it takes more than a year…
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 24
Step 2: Minhashing: Convert large sets to
short signatures, while preserving similarity
Docu-
ment
The set
of strings
of length k
that appear
in the doc-
ument
Signatures:
short integer
vectors that
represent the
sets, and
reflect their
similarity
 Many similarity problems can be
formalized as finding subsets that
have significant intersection
 Encode sets using 0/1 (bit, boolean) vectors
 One dimension per element in the universal set
 Interpret set intersection as bitwise AND, and
set union as bitwise OR
 Example: C1 = 10111; C2 = 10011
 Size of intersection = 3; size of union = 4,
 Jaccard similarity (not distance) = 3/4
 Distance: d(C1,C2) = 1 – (Jaccard similarity) = 1/4
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 26
 Rows = elements (shingles)
 Columns = sets (documents)
 1 in row e and column s if and only
if e is a member of s
 Column similarity is the Jaccard
similarity of the corresponding
sets (rows with value 1)
 Typical matrix is sparse!
 Each document is a column:
 Example: sim(C1 ,C2) = ?
 Size of intersection = 3; size of union = 6,
Jaccard similarity (not distance) = 3/6
 d(C1,C2) = 1 – (Jaccard similarity) = 3/6
27J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
0101
0111
1001
1000
1010
1011
0111
Documents
Shingles
 So far:
 Documents  Sets of shingles
 Represent sets as boolean vectors in a matrix
 Next goal: Find similar columns while
computing small signatures
 Similarity of columns == similarity of signatures
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 28
 Next Goal: Find similar columns, Small signatures
 Naïve approach:
 1) Signatures of columns: small summaries of columns
 2) Examine pairs of signatures to find similar columns
 Essential: Similarities of signatures and columns are related
 3) Optional: Check that columns with similar signatures
are really similar
 Warnings:
 Comparing all pairs may take too much time: Job for LSH
 These methods can produce false negatives, and even false
positives (if the optional check is not made)
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 29
 Key idea: “hash” each column C to a small
signature h(C), such that:
 (1) h(C) is small enough that the signature fits in RAM
 (2) sim(C1, C2) is the same as the “similarity” of
signatures h(C1) and h(C2)
 Goal: Find a hash function h(·) such that:
 If sim(C1,C2) is high, then with high prob. h(C1) = h(C2)
 If sim(C1,C2) is low, then with high prob. h(C1) ≠ h(C2)
 Hash docs into buckets. Expect that “most” pairs
of near duplicate docs hash into the same bucket!
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 30
 Goal: Find a hash function h(·) such that:
 if sim(C1,C2) is high, then with high prob. h(C1) = h(C2)
 if sim(C1,C2) is low, then with high prob. h(C1) ≠ h(C2)
 Clearly, the hash function depends on
the similarity metric:
 Not all similarity metrics have a suitable
hash function
 There is a suitable hash function for
the Jaccard similarity: It is called Min-Hashing
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 31
32
 Imagine the rows of the boolean matrix
permuted under random permutation 
 Define a “hash” function h(C) = the index of
the first (in the permuted order ) row in
which column C has value 1:
h (C) = min (C)
 Use several (e.g., 100) independent hash
functions (that is, permutations) to create a
signature of a column
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
33
3
4
7
2
6
1
5
Signature matrix M
1212
5
7
6
3
1
2
4
1412
4
5
1
6
7
3
2
2121
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
2nd element of the permutation
is the first to map to a 1
4th element of the permutation
is the first to map to a 1
0101
0101
1010
1010
1010
1001
0101
Input matrix (Shingles x Documents)Permutation 
Note: Another (equivalent) way is to
store row indexes: 1 5 1 5
2 3 1 3
6 4 6 4
 Choose a random permutation 
 Claim: Pr[h(C1) = h(C2)] = sim(C1, C2)
 Why?
 Let X be a doc (set of shingles), y X is a shingle
 Then: Pr[(y) = min((X))] = 1/|X|
 It is equally likely that any y X is mapped to the min element
 Let y be s.t. (y) = min((C1C2))
 Then either: (y) = min((C1)) if y  C1 , or
(y) = min((C2)) if y  C2
 So the prob. that both are true is the prob. y  C1  C2
 Pr[min((C1))=min((C2))]=|C1C2|/|C1C2|= sim(C1, C2)
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 34
01
10
00
11
00
00
One of the two
cols had to have
1 at position y
36
 We know: Pr[h(C1) = h(C2)] = sim(C1, C2)
 Now generalize to multiple hash functions
 The similarity of two signatures is the
fraction of the hash functions in which they
agree
 Note: Because of the Min-Hash property, the
similarity of columns is the same as the
expected similarity of their signatures
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
37J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
Similarities:
1-3 2-4 1-2 3-4
Col/Col 0.75 0.75 0 0
Sig/Sig 0.67 1.00 0 0
Signature matrix M
1212
5
7
6
3
1
2
4
1412
4
5
1
6
7
3
2
2121
0101
0101
1010
1010
1010
1001
0101
Input matrix (Shingles x Documents)
3
4
7
2
6
1
5
Permutation 
 Pick K=100 random permutations of the rows
 Think of sig(C) as a column vector
 sig(C)[i] = according to the i-th permutation, the
index of the first row that has a 1 in column C
sig(C)[i] = min (i(C))
 Note: The sketch (signature) of document C is
small ~𝟏𝟎𝟎 bytes!
 We achieved our goal! We “compressed”
long bit vectors into short signatures
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 38
 Permuting rows even once is prohibitive
 Row hashing!
 Pick K = 100 hash functions ki
 Ordering under ki gives a random row permutation!
 One-pass implementation
 For each column C and hash-func. ki keep a “slot” for
the min-hash value
 Initialize all sig(C)[i] = 
 Scan rows looking for 1s
 Suppose row j has 1 in column C
 Then for each ki :
 If ki(j) < sig(C)[i], then sig(C)[i]  ki(j)
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 39
How to pick a random
hash function h(x)?
Universal hashing:
ha,b(x)=((a·x+b) mod p) mod N
where:
a,b … random integers
p … prime number (p > N)
Step 3: Locality-Sensitive Hashing:
Focus on pairs of signatures likely to be from
similar documents
Docu-
ment
The set
of strings
of length k
that appear
in the doc-
ument
Signatures:
short integer
vectors that
represent the
sets, and
reflect their
similarity
Locality-
Sensitive
Hashing
Candidate
pairs:
those pairs
of signatures
that we need
to test for
similarity
 Goal: Find documents with Jaccard similarity at
least s (for some similarity threshold, e.g., s=0.8)
 LSH – General idea: Use a function f(x,y) that
tells whether x and y is a candidate pair: a pair
of elements whose similarity must be evaluated
 For Min-Hash matrices:
 Hash columns of signature matrix M to many buckets
 Each pair of documents that hashes into the
same bucket is a candidate pair
41J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
1212
1412
2121
 Pick a similarity threshold s (0 < s < 1)
 Columns x and y of M are a candidate pair if
their signatures agree on at least fraction s of
their rows:
M (i, x) = M (i, y) for at least frac. s values of i
 We expect documents x and y to have the same
(Jaccard) similarity as their signatures
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 42
1212
1412
2121
 Big idea: Hash columns of
signature matrix M several times
 Arrange that (only) similar columns are
likely to hash to the same bucket, with
high probability
 Candidate pairs are those that hash to
the same bucket
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 43
1212
1412
2121
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 44
Signature matrix M
r rows
per band
b bands
One
signature
1212
1412
2121
 Divide matrix M into b bands of r rows
 For each band, hash its portion of each
column to a hash table with k buckets
 Make k as large as possible
 Candidate column pairs are those that hash
to the same bucket for ≥ 1 band
 Tune b and r to catch most similar pairs,
but few non-similar pairs
45J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
Matrix M
r rows b bands
Buckets
Columns 2 and 6
are probably identical
(candidate pair)
Columns 6 and 7 are
surely different.
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 46
 There are enough buckets that columns are
unlikely to hash to the same bucket unless
they are identical in a particular band
 Hereafter, we assume that “same bucket”
means “identical in that band”
 Assumption needed only to simplify analysis,
not for correctness of algorithm
47J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
Assume the following case:
 Suppose 100,000 columns of M (100k docs)
 Signatures of 100 integers (rows)
 Therefore, signatures take 40Mb
 Choose b = 20 bands of r = 5 integers/band
 Goal: Find pairs of documents that
are at least s = 0.8 similar
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 48
1212
1412
2121
 Find pairs of  s=0.8 similarity, set b=20, r=5
 Assume: sim(C1, C2) = 0.8
 Since sim(C1, C2)  s, we want C1, C2 to be a candidate
pair: We want them to hash to at least 1 common bucket
(at least one band is identical)
 Probability C1, C2 identical in one particular
band: (0.8)5 = 0.328
 Probability C1, C2 are not similar in all of the 20
bands: (1-0.328)20 = 0.00035
 i.e., about 1/3000th of the 80%-similar column pairs
are false negatives (we miss them)
 We would find 99.965% pairs of truly similar documents
49J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
1212
1412
2121
 Find pairs of  s=0.8 similarity, set b=20, r=5
 Assume: sim(C1, C2) = 0.3
 Since sim(C1, C2) < s we want C1, C2 to hash to NO
common buckets (all bands should be different)
 Probability C1, C2 identical in one particular
band: (0.3)5 = 0.00243
 Probability C1, C2 identical in at least 1 of 20
bands: 1 - (1 - 0.00243)20 = 0.0474
 In other words, approximately 4.74% pairs of docs
with similarity 0.3% end up becoming candidate pairs
 They are false positives since we will have to examine them
(they are candidate pairs) but then it will turn out their
similarity is below threshold s
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 50
1212
1412
2121
 Pick:
 The number of Min-Hashes (rows of M)
 The number of bands b, and
 The number of rows r per band
to balance false positives/negatives
 Example: If we had only 15 bands of 5
rows, the number of false positives would
go down, but the number of false negatives
would go up
51J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
1212
1412
2121
Similarity t =sim(C1, C2) of two sets
Probability
of sharing
a bucket
Similaritythresholds
No chance
if t < s
Probability = 1
if t > s
52J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 53
Remember:
Probability of
equal hash-values
= similarity
Similarity t =sim(C1, C2) of two sets
Probability
of sharing
a bucket
 Columns C1 and C2 have similarity t
 Pick any band (r rows)
 Prob. that all rows in band equal = tr
 Prob. that some row in band unequal = 1 - tr
 Prob. that no band identical = (1 - tr)b
 Prob. that at least 1 band identical =
1 - (1 - tr)b
54J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
t r
All rows
of a band
are equal
1 -
Some row
of a band
unequal
( )b
No bands
identical
1 -
At least
one band
identical
s ~ (1/b)1/r
55J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
Similarity t=sim(C1, C2) of two sets
Probability
of sharing
a bucket
 Similarity threshold s
 Prob. that at least 1 band is identical:
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 56
s 1-(1-sr)b
.2 .006
.3 .047
.4 .186
.5 .470
.6 .802
.7 .975
.8 .9996
 Picking r and b to get the best S-curve
 50 hash-functions (r=5, b=10)
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 57
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Blue area: False Negative rate
Green area: False Positive rate
Similarity
Prob.sharingabucket
 Tune M, b, r to get almost all pairs with
similar signatures, but eliminate most pairs
that do not have similar signatures
 Check in main memory that candidate pairs
really do have similar signatures
 Optional: In another pass through data,
check that the remaining candidate pairs
really represent similar documents
58J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
 Shingling: Convert documents to sets
 We used hashing to assign each shingle an ID
 Min-Hashing: Convert large sets to short
signatures, while preserving similarity
 We used similarity preserving hashing to generate
signatures with property Pr[h(C1) = h(C2)] = sim(C1, C2)
 We used hashing to get around generating random
permutations
 Locality-Sensitive Hashing: Focus on pairs of
signatures likely to be from similar documents
 We used hashing to find candidate pairs of similarity  s
J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 59

More Related Content

What's hot (20)

PPTX
Clustering paradigms and Partitioning Algorithms
Umang MIshra
 
PDF
Introduction to text classification using naive bayes
Dhwaj Raj
 
PDF
Breast cancerdetection IE594 Project Report
ASHISH MENKUDALE
 
PPTX
Dimensionality reduction: SVD and its applications
Viet-Trung TRAN
 
PDF
An Introduction to Supervised Machine Learning and Pattern Classification: Th...
Sebastian Raschka
 
PDF
Evaluation in Information Retrieval
Dishant Ailawadi
 
PPT
2.4 rule based classification
Krish_ver2
 
PPTX
Eclat algorithm in association rule mining
Deepa Jeya
 
PPT
5.3 mining sequential patterns
Krish_ver2
 
PPTX
Classification Algorithm.
Megha Sharma
 
PPT
Chapter - 8.3 Data Mining Concepts and Techniques 2nd Ed slides Han & Kamber
error007
 
PPT
08. Object Oriented Database in DBMS
koolkampus
 
PPTX
Neural network
Babu Priyavrat
 
PPT
3. Relational Models in DBMS
koolkampus
 
PPTX
Ensemble learning
Haris Jamil
 
PPTX
K-Nearest Neighbor(KNN)
Abdullah al Mamun
 
PPTX
Deep Learning - CNN and RNN
Ashray Bhandare
 
PDF
Distributed machine learning
Stanley Wang
 
PPTX
Week-1-Introduction to Data Mining.pptx
Take1As
 
PDF
Convolutional neural network
Itachi SK
 
Clustering paradigms and Partitioning Algorithms
Umang MIshra
 
Introduction to text classification using naive bayes
Dhwaj Raj
 
Breast cancerdetection IE594 Project Report
ASHISH MENKUDALE
 
Dimensionality reduction: SVD and its applications
Viet-Trung TRAN
 
An Introduction to Supervised Machine Learning and Pattern Classification: Th...
Sebastian Raschka
 
Evaluation in Information Retrieval
Dishant Ailawadi
 
2.4 rule based classification
Krish_ver2
 
Eclat algorithm in association rule mining
Deepa Jeya
 
5.3 mining sequential patterns
Krish_ver2
 
Classification Algorithm.
Megha Sharma
 
Chapter - 8.3 Data Mining Concepts and Techniques 2nd Ed slides Han & Kamber
error007
 
08. Object Oriented Database in DBMS
koolkampus
 
Neural network
Babu Priyavrat
 
3. Relational Models in DBMS
koolkampus
 
Ensemble learning
Haris Jamil
 
K-Nearest Neighbor(KNN)
Abdullah al Mamun
 
Deep Learning - CNN and RNN
Ashray Bhandare
 
Distributed machine learning
Stanley Wang
 
Week-1-Introduction to Data Mining.pptx
Take1As
 
Convolutional neural network
Itachi SK
 

Similar to Ch03 Mining Massive Data Sets stanford (20)

PPTX
ch03-lsh.pptxCS246: Mining Massive Datasets Jure Leskovec, Stanford Universi...
sidok73251
 
PPTX
3 - Finding similar items
Viet-Trung TRAN
 
PDF
04-lsh_theory.pdfCS246: Mining Massive Datasets Jure Leskovec, Stanford Univ...
sidok73251
 
PPT
similarity1 (6).ppt
ssuserf11a32
 
PPTX
ch04-streams1.pptxCS246: Mining Massive Datasets Jure Leskovec, Stanford Uni...
sidok73251
 
PPTX
ch10-graphs2.pptx
arkian3
 
PPTX
Mining of massive datasets
Ashic Mahtab
 
PDF
ch10-graphs2.pdf
ssuserf11a32
 
PDF
Bando de Dados Avançados - Recommender Systems
Gustavo Coutinho
 
PPTX
ch09-recsys1.pptxMust examine each pair of drugs and compare their data.
lobiki7243
 
PPTX
Mining massive datasets using recommender system
rosni
 
PDF
Frequent Itemset Minning and Association Rules
kasorikm
 
PDF
Recommender Systems Content and Collaborative Filtering
rosni
 
PDF
Locality sensitive hashing
SEMINARGROOT
 
PDF
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
Mail.ru Group
 
PDF
Finding similar items in high dimensional spaces locality sensitive hashing
Dmitriy Selivanov
 
PPT
Lecture20
mattriley
 
PDF
Probabilistic data structures. Part 4. Similarity
Andrii Gakhov
 
PDF
Approximate methods for scalable data mining (long version)
Andrew Clegg
 
PPT
Datamining tools and techniques_lec-2.ppt
SCIENCEQBHHSS
 
ch03-lsh.pptxCS246: Mining Massive Datasets Jure Leskovec, Stanford Universi...
sidok73251
 
3 - Finding similar items
Viet-Trung TRAN
 
04-lsh_theory.pdfCS246: Mining Massive Datasets Jure Leskovec, Stanford Univ...
sidok73251
 
similarity1 (6).ppt
ssuserf11a32
 
ch04-streams1.pptxCS246: Mining Massive Datasets Jure Leskovec, Stanford Uni...
sidok73251
 
ch10-graphs2.pptx
arkian3
 
Mining of massive datasets
Ashic Mahtab
 
ch10-graphs2.pdf
ssuserf11a32
 
Bando de Dados Avançados - Recommender Systems
Gustavo Coutinho
 
ch09-recsys1.pptxMust examine each pair of drugs and compare their data.
lobiki7243
 
Mining massive datasets using recommender system
rosni
 
Frequent Itemset Minning and Association Rules
kasorikm
 
Recommender Systems Content and Collaborative Filtering
rosni
 
Locality sensitive hashing
SEMINARGROOT
 
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
Mail.ru Group
 
Finding similar items in high dimensional spaces locality sensitive hashing
Dmitriy Selivanov
 
Lecture20
mattriley
 
Probabilistic data structures. Part 4. Similarity
Andrii Gakhov
 
Approximate methods for scalable data mining (long version)
Andrew Clegg
 
Datamining tools and techniques_lec-2.ppt
SCIENCEQBHHSS
 
Ad

Recently uploaded (20)

PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Ad

Ch03 Mining Massive Data Sets stanford

  • 1. Mining of Massive Datasets Jure Leskovec, Anand Rajaraman, Jeff Ullman Stanford University http://www.mmds.org Note to other teachers and users of these slides: We would be delighted if you found this our material useful in giving your own lectures. Feel free to use these slides verbatim, or to modify them to fit your own needs. If you make use of a significant portion of these slides in your own lecture, please include this message, or a link to our web site: http://www.mmds.org
  • 2. High dim. data Locality sensitive hashing Clustering Dimensional ity reduction Graph data PageRank, SimRank Network Analysis Spam Detection Infinite data Filtering data streams Web advertising Queries on streams Machine learning SVM Decision Trees Perceptron, kNN Apps Recommen der systems Association Rules Duplicate document detection J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 2
  • 3. 3J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org [Hays and Efros, SIGGRAPH 2007]
  • 4. 4J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org [Hays and Efros, SIGGRAPH 2007]
  • 5. 10 nearest neighbors from a collection of 20,000 images J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 5 [Hays and Efros, SIGGRAPH 2007]
  • 6. 10 nearest neighbors from a collection of 2 million images J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 6 [Hays and Efros, SIGGRAPH 2007]
  • 7.  Many problems can be expressed as finding “similar” sets:  Find near-neighbors in high-dimensional space  Examples:  Pages with similar words  For duplicate detection, classification by topic  Customers who purchased similar products  Products with similar customer sets  Images with similar features  Users who visited similar websites J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 7
  • 8.  Given: High dimensional data points 𝒙 𝟏, 𝒙 𝟐, …  For example: Image is a long vector of pixel colors 1 2 1 0 2 1 0 1 0 → [1 2 1 0 2 1 0 1 0]  And some distance function 𝒅(𝒙 𝟏, 𝒙 𝟐)  Which quantifies the “distance” between 𝒙 𝟏 and 𝒙 𝟐  Goal: Find all pairs of data points (𝒙𝒊, 𝒙𝒋) that are within some distance threshold 𝒅 𝒙𝒊, 𝒙𝒋 ≤ 𝒔  Note: Naïve solution would take 𝑶 𝑵 𝟐  where 𝑵 is the number of data points  MAGIC: This can be done in 𝑶 𝑵 !! How? J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 8
  • 9.  Last time: Finding frequent pairs J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 9 Items1…N Items 1…N Count of pair {i,j} in the data Naïve solution: Single pass but requires space quadratic in the number of items Items1…K Items 1…K Count of pair {i,j} in the data A-Priori: First pass: Find frequent singletons For a pair to be a frequent pair candidate, its singletons have to be frequent! Second pass: Count only candidate pairs! N … number of distinct items K … number of items with support  s
  • 10.  Last time: Finding frequent pairs  Further improvement: PCY  Pass 1:  Count exact frequency of each item:  Take pairs of items {i,j}, hash them into B buckets and count of the number of pairs that hashed to each bucket: J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 10 Items 1…N Basket 1: {1,2,3} Pairs: {1,2} {1,3} {2,3} Buckets 1…B 2 1
  • 11.  Last time: Finding frequent pairs  Further improvement: PCY  Pass 1:  Count exact frequency of each item:  Take pairs of items {i,j}, hash them into B buckets and count of the number of pairs that hashed to each bucket:  Pass 2:  For a pair {i,j} to be a candidate for a frequent pair, its singletons {i}, {j} have to be frequent and the pair has to hash to a frequent bucket! J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 11 Items 1…N Basket 1: {1,2,3} Pairs: {1,2} {1,3} {2,3} Basket 2: {1,2,4} Pairs: {1,2} {1,4} {2,4} Buckets 1…B 3 1 2
  • 12.  Last time: Finding frequent pairs  Further improvement: PCY  Pass 1:  Count exact frequency of each item:  Take pairs of items {i,j}, hash them into B buckets and count of the number of pairs that hashed to each bucket:  Pass 2:  For a pair {i,j} to be a candidate for a frequent pair, its singletons have to be frequent and its has to hash to a frequent bucket! J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 12 Items 1…N Basket 1: {1,2,3} Pairs: {1,2} {1,3} {2,3} Basket 2: {1,2,4} Pairs: {1,2} {1,4} {2,4} Buckets 1…B 3 1 2 Previous lecture: A-Priori Main idea: Candidates Instead of keeping a count of each pair, only keep a count of candidate pairs! Today’s lecture: Find pairs of similar docs Main idea: Candidates -- Pass 1:Take documents and hash them to buckets such that documents that are similar hash to the same bucket -- Pass 2: Only compare documents that are candidates (i.e., they hashed to a same bucket) Benefits: Instead of O(N2) comparisons, we need O(N) comparisons to find similar documents
  • 14.  Goal: Find near-neighbors in high-dim. space  We formally define “near neighbors” as points that are a “small distance” apart  For each application, we first need to define what “distance” means  Today: Jaccard distance/similarity  The Jaccard similarity of two sets is the size of their intersection divided by the size of their union: sim(C1, C2) = |C1C2|/|C1C2|  Jaccard distance: d(C1, C2) = 1 - |C1C2|/|C1C2| 14J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 3 in intersection 8 in union Jaccard similarity= 3/8 Jaccard distance = 5/8
  • 15.  Goal: Given a large number (𝑵 in the millions or billions) of documents, find “near duplicate” pairs  Applications:  Mirror websites, or approximate mirrors  Don’t want to show both in search results  Similar news articles at many news sites  Cluster articles by “same story”  Problems:  Many small pieces of one document can appear out of order in another  Too many documents to compare all pairs  Documents are so large or so many that they cannot fit in main memory J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 15
  • 16. 1. Shingling: Convert documents to sets 2. Min-Hashing: Convert large sets to short signatures, while preserving similarity 3. Locality-Sensitive Hashing: Focus on pairs of signatures likely to be from similar documents  Candidate pairs! J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 16
  • 17. 17 Docu- ment The set of strings of length k that appear in the doc- ument Signatures: short integer vectors that represent the sets, and reflect their similarity Locality- Sensitive Hashing Candidate pairs: those pairs of signatures that we need to test for similarity J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
  • 18. Step 1: Shingling: Convert documents to sets Docu- ment The set of strings of length k that appear in the doc- ument
  • 19.  Step 1: Shingling: Convert documents to sets  Simple approaches:  Document = set of words appearing in document  Document = set of “important” words  Don’t work well for this application. Why?  Need to account for ordering of words!  A different way: Shingles! J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 19
  • 20.  A k-shingle (or k-gram) for a document is a sequence of k tokens that appears in the doc  Tokens can be characters, words or something else, depending on the application  Assume tokens = characters for examples  Example: k=2; document D1 = abcab Set of 2-shingles: S(D1) = {ab, bc, ca}  Option: Shingles as a bag (multiset), count ab twice: S’(D1) = {ab, bc, ca, ab} J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 20
  • 21.  To compress long shingles, we can hash them to (say) 4 bytes  Represent a document by the set of hash values of its k-shingles  Idea: Two documents could (rarely) appear to have shingles in common, when in fact only the hash- values were shared  Example: k=2; document D1= abcab Set of 2-shingles: S(D1) = {ab, bc, ca} Hash the singles: h(D1) = {1, 5, 7} J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 21
  • 22.  Document D1 is a set of its k-shingles C1=S(D1)  Equivalently, each document is a 0/1 vector in the space of k-shingles  Each unique shingle is a dimension  Vectors are very sparse  A natural similarity measure is the Jaccard similarity: sim(D1, D2) = |C1C2|/|C1C2| J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 22
  • 23.  Documents that have lots of shingles in common have similar text, even if the text appears in different order  Caveat: You must pick k large enough, or most documents will have most shingles  k = 5 is OK for short documents  k = 10 is better for long documents J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 23
  • 24.  Suppose we need to find near-duplicate documents among 𝑵 = 𝟏 million documents  Naïvely, we would have to compute pairwise Jaccard similarities for every pair of docs  𝑵(𝑵 − 𝟏)/𝟐 ≈ 5*1011 comparisons  At 105 secs/day and 106 comparisons/sec, it would take 5 days  For 𝑵 = 𝟏𝟎 million, it takes more than a year… J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 24
  • 25. Step 2: Minhashing: Convert large sets to short signatures, while preserving similarity Docu- ment The set of strings of length k that appear in the doc- ument Signatures: short integer vectors that represent the sets, and reflect their similarity
  • 26.  Many similarity problems can be formalized as finding subsets that have significant intersection  Encode sets using 0/1 (bit, boolean) vectors  One dimension per element in the universal set  Interpret set intersection as bitwise AND, and set union as bitwise OR  Example: C1 = 10111; C2 = 10011  Size of intersection = 3; size of union = 4,  Jaccard similarity (not distance) = 3/4  Distance: d(C1,C2) = 1 – (Jaccard similarity) = 1/4 J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 26
  • 27.  Rows = elements (shingles)  Columns = sets (documents)  1 in row e and column s if and only if e is a member of s  Column similarity is the Jaccard similarity of the corresponding sets (rows with value 1)  Typical matrix is sparse!  Each document is a column:  Example: sim(C1 ,C2) = ?  Size of intersection = 3; size of union = 6, Jaccard similarity (not distance) = 3/6  d(C1,C2) = 1 – (Jaccard similarity) = 3/6 27J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 0101 0111 1001 1000 1010 1011 0111 Documents Shingles
  • 28.  So far:  Documents  Sets of shingles  Represent sets as boolean vectors in a matrix  Next goal: Find similar columns while computing small signatures  Similarity of columns == similarity of signatures J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 28
  • 29.  Next Goal: Find similar columns, Small signatures  Naïve approach:  1) Signatures of columns: small summaries of columns  2) Examine pairs of signatures to find similar columns  Essential: Similarities of signatures and columns are related  3) Optional: Check that columns with similar signatures are really similar  Warnings:  Comparing all pairs may take too much time: Job for LSH  These methods can produce false negatives, and even false positives (if the optional check is not made) J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 29
  • 30.  Key idea: “hash” each column C to a small signature h(C), such that:  (1) h(C) is small enough that the signature fits in RAM  (2) sim(C1, C2) is the same as the “similarity” of signatures h(C1) and h(C2)  Goal: Find a hash function h(·) such that:  If sim(C1,C2) is high, then with high prob. h(C1) = h(C2)  If sim(C1,C2) is low, then with high prob. h(C1) ≠ h(C2)  Hash docs into buckets. Expect that “most” pairs of near duplicate docs hash into the same bucket! J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 30
  • 31.  Goal: Find a hash function h(·) such that:  if sim(C1,C2) is high, then with high prob. h(C1) = h(C2)  if sim(C1,C2) is low, then with high prob. h(C1) ≠ h(C2)  Clearly, the hash function depends on the similarity metric:  Not all similarity metrics have a suitable hash function  There is a suitable hash function for the Jaccard similarity: It is called Min-Hashing J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 31
  • 32. 32  Imagine the rows of the boolean matrix permuted under random permutation   Define a “hash” function h(C) = the index of the first (in the permuted order ) row in which column C has value 1: h (C) = min (C)  Use several (e.g., 100) independent hash functions (that is, permutations) to create a signature of a column J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
  • 33. 33 3 4 7 2 6 1 5 Signature matrix M 1212 5 7 6 3 1 2 4 1412 4 5 1 6 7 3 2 2121 J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 2nd element of the permutation is the first to map to a 1 4th element of the permutation is the first to map to a 1 0101 0101 1010 1010 1010 1001 0101 Input matrix (Shingles x Documents)Permutation  Note: Another (equivalent) way is to store row indexes: 1 5 1 5 2 3 1 3 6 4 6 4
  • 34.  Choose a random permutation   Claim: Pr[h(C1) = h(C2)] = sim(C1, C2)  Why?  Let X be a doc (set of shingles), y X is a shingle  Then: Pr[(y) = min((X))] = 1/|X|  It is equally likely that any y X is mapped to the min element  Let y be s.t. (y) = min((C1C2))  Then either: (y) = min((C1)) if y  C1 , or (y) = min((C2)) if y  C2  So the prob. that both are true is the prob. y  C1  C2  Pr[min((C1))=min((C2))]=|C1C2|/|C1C2|= sim(C1, C2) J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 34 01 10 00 11 00 00 One of the two cols had to have 1 at position y
  • 35. 36  We know: Pr[h(C1) = h(C2)] = sim(C1, C2)  Now generalize to multiple hash functions  The similarity of two signatures is the fraction of the hash functions in which they agree  Note: Because of the Min-Hash property, the similarity of columns is the same as the expected similarity of their signatures J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
  • 36. 37J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org Similarities: 1-3 2-4 1-2 3-4 Col/Col 0.75 0.75 0 0 Sig/Sig 0.67 1.00 0 0 Signature matrix M 1212 5 7 6 3 1 2 4 1412 4 5 1 6 7 3 2 2121 0101 0101 1010 1010 1010 1001 0101 Input matrix (Shingles x Documents) 3 4 7 2 6 1 5 Permutation 
  • 37.  Pick K=100 random permutations of the rows  Think of sig(C) as a column vector  sig(C)[i] = according to the i-th permutation, the index of the first row that has a 1 in column C sig(C)[i] = min (i(C))  Note: The sketch (signature) of document C is small ~𝟏𝟎𝟎 bytes!  We achieved our goal! We “compressed” long bit vectors into short signatures J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 38
  • 38.  Permuting rows even once is prohibitive  Row hashing!  Pick K = 100 hash functions ki  Ordering under ki gives a random row permutation!  One-pass implementation  For each column C and hash-func. ki keep a “slot” for the min-hash value  Initialize all sig(C)[i] =   Scan rows looking for 1s  Suppose row j has 1 in column C  Then for each ki :  If ki(j) < sig(C)[i], then sig(C)[i]  ki(j) J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 39 How to pick a random hash function h(x)? Universal hashing: ha,b(x)=((a·x+b) mod p) mod N where: a,b … random integers p … prime number (p > N)
  • 39. Step 3: Locality-Sensitive Hashing: Focus on pairs of signatures likely to be from similar documents Docu- ment The set of strings of length k that appear in the doc- ument Signatures: short integer vectors that represent the sets, and reflect their similarity Locality- Sensitive Hashing Candidate pairs: those pairs of signatures that we need to test for similarity
  • 40.  Goal: Find documents with Jaccard similarity at least s (for some similarity threshold, e.g., s=0.8)  LSH – General idea: Use a function f(x,y) that tells whether x and y is a candidate pair: a pair of elements whose similarity must be evaluated  For Min-Hash matrices:  Hash columns of signature matrix M to many buckets  Each pair of documents that hashes into the same bucket is a candidate pair 41J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 1212 1412 2121
  • 41.  Pick a similarity threshold s (0 < s < 1)  Columns x and y of M are a candidate pair if their signatures agree on at least fraction s of their rows: M (i, x) = M (i, y) for at least frac. s values of i  We expect documents x and y to have the same (Jaccard) similarity as their signatures J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 42 1212 1412 2121
  • 42.  Big idea: Hash columns of signature matrix M several times  Arrange that (only) similar columns are likely to hash to the same bucket, with high probability  Candidate pairs are those that hash to the same bucket J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 43 1212 1412 2121
  • 43. J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 44 Signature matrix M r rows per band b bands One signature 1212 1412 2121
  • 44.  Divide matrix M into b bands of r rows  For each band, hash its portion of each column to a hash table with k buckets  Make k as large as possible  Candidate column pairs are those that hash to the same bucket for ≥ 1 band  Tune b and r to catch most similar pairs, but few non-similar pairs 45J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
  • 45. Matrix M r rows b bands Buckets Columns 2 and 6 are probably identical (candidate pair) Columns 6 and 7 are surely different. J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 46
  • 46.  There are enough buckets that columns are unlikely to hash to the same bucket unless they are identical in a particular band  Hereafter, we assume that “same bucket” means “identical in that band”  Assumption needed only to simplify analysis, not for correctness of algorithm 47J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
  • 47. Assume the following case:  Suppose 100,000 columns of M (100k docs)  Signatures of 100 integers (rows)  Therefore, signatures take 40Mb  Choose b = 20 bands of r = 5 integers/band  Goal: Find pairs of documents that are at least s = 0.8 similar J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 48 1212 1412 2121
  • 48.  Find pairs of  s=0.8 similarity, set b=20, r=5  Assume: sim(C1, C2) = 0.8  Since sim(C1, C2)  s, we want C1, C2 to be a candidate pair: We want them to hash to at least 1 common bucket (at least one band is identical)  Probability C1, C2 identical in one particular band: (0.8)5 = 0.328  Probability C1, C2 are not similar in all of the 20 bands: (1-0.328)20 = 0.00035  i.e., about 1/3000th of the 80%-similar column pairs are false negatives (we miss them)  We would find 99.965% pairs of truly similar documents 49J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 1212 1412 2121
  • 49.  Find pairs of  s=0.8 similarity, set b=20, r=5  Assume: sim(C1, C2) = 0.3  Since sim(C1, C2) < s we want C1, C2 to hash to NO common buckets (all bands should be different)  Probability C1, C2 identical in one particular band: (0.3)5 = 0.00243  Probability C1, C2 identical in at least 1 of 20 bands: 1 - (1 - 0.00243)20 = 0.0474  In other words, approximately 4.74% pairs of docs with similarity 0.3% end up becoming candidate pairs  They are false positives since we will have to examine them (they are candidate pairs) but then it will turn out their similarity is below threshold s J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 50 1212 1412 2121
  • 50.  Pick:  The number of Min-Hashes (rows of M)  The number of bands b, and  The number of rows r per band to balance false positives/negatives  Example: If we had only 15 bands of 5 rows, the number of false positives would go down, but the number of false negatives would go up 51J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 1212 1412 2121
  • 51. Similarity t =sim(C1, C2) of two sets Probability of sharing a bucket Similaritythresholds No chance if t < s Probability = 1 if t > s 52J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
  • 52. J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 53 Remember: Probability of equal hash-values = similarity Similarity t =sim(C1, C2) of two sets Probability of sharing a bucket
  • 53.  Columns C1 and C2 have similarity t  Pick any band (r rows)  Prob. that all rows in band equal = tr  Prob. that some row in band unequal = 1 - tr  Prob. that no band identical = (1 - tr)b  Prob. that at least 1 band identical = 1 - (1 - tr)b 54J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
  • 54. t r All rows of a band are equal 1 - Some row of a band unequal ( )b No bands identical 1 - At least one band identical s ~ (1/b)1/r 55J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org Similarity t=sim(C1, C2) of two sets Probability of sharing a bucket
  • 55.  Similarity threshold s  Prob. that at least 1 band is identical: J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 56 s 1-(1-sr)b .2 .006 .3 .047 .4 .186 .5 .470 .6 .802 .7 .975 .8 .9996
  • 56.  Picking r and b to get the best S-curve  50 hash-functions (r=5, b=10) J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 57 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Blue area: False Negative rate Green area: False Positive rate Similarity Prob.sharingabucket
  • 57.  Tune M, b, r to get almost all pairs with similar signatures, but eliminate most pairs that do not have similar signatures  Check in main memory that candidate pairs really do have similar signatures  Optional: In another pass through data, check that the remaining candidate pairs really represent similar documents 58J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org
  • 58.  Shingling: Convert documents to sets  We used hashing to assign each shingle an ID  Min-Hashing: Convert large sets to short signatures, while preserving similarity  We used similarity preserving hashing to generate signatures with property Pr[h(C1) = h(C2)] = sim(C1, C2)  We used hashing to get around generating random permutations  Locality-Sensitive Hashing: Focus on pairs of signatures likely to be from similar documents  We used hashing to find candidate pairs of similarity  s J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 59

Editor's Notes

  • #5: 10 nearest neighbors, 20,000 image database
  • #6: 10 nearest neighbors, 20,000 image database
  • #7: 10 nearest neighbors, 2.3 million image database
  • #35: Size of the universe of all possible vals of min((C1C2)) is |C1C2| and in |C1C2| of cases it can be that min((C1))=min((C2)) which exactly the jaccard between C1 and C2 For two columns A and B, we have h_π(A) = h_π(B) exactly when the minimum hash value of the union A ∪ B lies in the intersection A ∩ B. Thus Pr[h_π(A) = h_π(B)] = |A ∩ B| / |A ∪ B|.
  • #37: Each agress with prob s. So to estimate s we compute what fraction of hash functions agree