Sub topic 5
The MinHash value of a set
This chapter assumes you know union, intersection, and Jaccard similarity. It also assumes you know that a hash function maps an item to a number, and that you can read short Python programs. The previous chapter showed how a hash function gives every element a position in a random ordering. Here we use that ordering to define the MinHash value of a set.
The definition
Fix an ordering of every element that could appear in any set. The universe is the collection of all those possible elements. The MinHash value of a set under that ordering is the element of the set that comes first in the ordering.
Two properties make this useful:
- The value depends on the ordering. A different random ordering usually gives a different value for the same set.
- For a fixed ordering, the value is a single item. A set of a million elements is summarized by one element.
The diagram shows the process for one ordering.
A worked example
Take two sets over the universe a, b, c, d, e:
- A = {a, b, c}
- B = {b, c, d}
Their intersection is {b, c} and their union is {a, b, c, d}, so the Jaccard similarity is 2/4 = 0.5.
The code below applies two different orderings to both sets.
def minhash(s, order):
rank = {x: i for i, x in enumerate(order)}
return min(s, key=rank.__getitem__)
A = {"a", "b", "c"}
B = {"b", "c", "d"}
order1 = ["d", "b", "e", "a", "c"]
order2 = ["c", "a", "d", "b", "e"]
print(minhash(A, order1), minhash(B, order1)) # b d
print(minhash(A, order2), minhash(B, order2)) # c cUnder order1, scanning from the front finds d first for B. d is not in A, so A's first element is b. The values differ.
Under order2, the element c is first in the ordering among all of A and B. Both sets contain it, so both values are c. The values match.
Where the first element comes from
Look at the union {a, b, c, d}. In each ordering, one element of the union comes before the others. Call it the union's front element.
- Each set's MinHash value is the first of its own elements in the ordering. If the union's front element belongs to a set, that element is the set's value.
- If the front element is in the intersection, both sets contain it, so both values equal it. The values match.
- If the front element is in only one set, that set's value is the front element. The other set's value is some later element, so the values differ.
This is what ties the match event to the Jaccard similarity. The next chapter turns it into a probability.
From orderings to hash values
Real systems do not store an ordering of the whole universe. The universe could be every possible 3-word phrase, which is far too large to list. Instead, a hash function maps each element to an integer, and the integers define the ordering. Smaller hash values come first.
The usual implementation therefore keeps the smallest hash value over the set, not the element itself:
import zlib
def minhash_value(items, a, b, p=(1 << 61) - 1):
return min((a * zlib.crc32(x.encode()) + b) % p for x in items)
print(minhash_value({"a", "b", "c"}, a=7, b=3))
print(minhash_value({"b", "c", "d"}, a=7, b=3))Two sets give the same minimum hash value exactly when the same element achieves the minimum in both, apart from rare hash collisions. Comparing hash values therefore stands in for comparing elements.
Common mistakes
- Using different orderings for the two sets. The match argument above only holds when both sets are scanned with the same ordering. Seed or fix each hash function once and reuse it for every set.
- Treating one value as a similarity score. A single MinHash value either matches or it does not. It is one yes-or-no sample. The estimator chapter combines many of them.
- Hashing with a weak function. The ordering must look random. A hash that sends similar elements to nearby values, such as using the raw integer ID, biases which element comes first.
- Ignoring the empty set. An empty set has no first element, so it has no MinHash value. Code usually skips such documents or assigns a sentinel value.
Exercise
Use the ordering ["e", "c", "a", "d", "b"] on the sets A = {a, b, c} and B = {b, c, d}. What are the two MinHash values, and do they match?
Show answer
Scanning the ordering from the front, e is in neither set, so skip it. c is next and is in both sets. Both MinHash values are c, so they match. This works because the front element of the union, c, lies in the intersection.