Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't set threshold=1 when using MinHashLSH? #243

Open
brother-darion opened this issue Sep 29, 2024 · 1 comment
Open

Can't set threshold=1 when using MinHashLSH? #243

brother-darion opened this issue Sep 29, 2024 · 1 comment

Comments

@brother-darion
Copy link

Is it can't set threshold=1 when using MinHashLSH? I got error when I using MinHashLSH and set threshold=1 :

here is the code:

from datasketch import MinHash, MinHashLSH

set1 = set(['minhash', 'is', 'a', 'probabilistic', 'data', 'structure', 'for',
            'estimating', 'the', 'similarity', 'between', 'datasets'])
set2 = set(['minhash', 'is', 'a', 'probability', 'data', 'structure', 'for',
            'estimating', 'the', 'similarity', 'between', 'documents'])
set3 = set(['minhash', 'is', 'probability', 'data', 'structure', 'for',
            'estimating', 'the', 'similarity', 'between', 'documents'])

m1 = MinHash(num_perm=128)
m2 = MinHash(num_perm=128)
m3 = MinHash(num_perm=128)
for d in set1:
    m1.update(d.encode('utf8'))
for d in set2:
    m2.update(d.encode('utf8'))
for d in set3:
    m3.update(d.encode('utf8'))

# Create LSH index
lsh = MinHashLSH(threshold=1, num_perm=128)
lsh.insert("m2", m2)
lsh.insert("m3", m3)
result = lsh.query(m1)
print("Approximate neighbours with Jaccard similarity > 0.5", result)

and got error:

Traceback (most recent call last):
File "/opt/venv/test/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3508, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 21, in
lsh = MinHashLSH(threshold=1, num_perm=128)
File "/opt/venv/test/lib/python3.8/site-packages/datasketch/lsh.py", line 174, in init
raise ValueError("The number of bands are too small (b < 2)")
ValueError: The number of bands are too small (b < 2)

@cameron-toy
Copy link

There's no need to use LSH for threshold=1.0 since that would only match items with the exact same MinHash. Instead, you can store items in a dict keyed by the hash of the entire MinHash, or even skip using MinHash and just use a single hash of the entire item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants