ekzhu / datasketch

MinHash, LSH, LSH Forest, Weighted MinHash, HyperLogLog, HyperLogLog++, LSH Ensemble and HNSW
https://ekzhu.github.io/datasketch
MIT License
2.59k stars 296 forks source link

No result for top-k #35

Closed pandasMX closed 7 years ago

pandasMX commented 7 years ago

I was trying to use lshforest to get top-k matching results for a list of floats.

    m = [MinHash(num_perm=256) for i in range(len(features))]
    forest = MinHashLSHForest(num_perm=256)

    for i, ID, feature in zip(range(len(features)),keys,features):
        m[i].update(struct.pack("{}f".format(len(feature)), *feature))
        forest.add(ID, m[i])
    forest.index()

Here, ID is a string and feature is a list of floats. I saw #26 and converted the floats to bytes.

Below is the code for query in anther python file.

minhash = MinHash(num_perm=256)
minhash.update(struct.pack("{}f".format(len(query_features)), *query_features))

result = forest.query(minhash, 50)

The query_features has the same length as feature. But the result is an empty list, is there any thing wrong with my code? Thanks

ekzhu commented 7 years ago

I notice you update minhash just once, with a concatenation of all features. This effectively makes each minhash contains just one element in the set.

        m[i].update(struct.pack("{}f".format(len(feature)), *feature))

I think you should call update on every feature in a loop to create just one minhash.

Another thing, if your features are all floats, they are considered distinct by minhash even when they are close. e.g. 1.00001 and 1.001 are different in minhash, because they produce different bytes. And two sets {1.0001, 2.0001} and {0.9998, 2.01} are completely different and have a jaccard similarity of 0.

pandasMX commented 7 years ago

Thanks! I also noticed my problem. However, after i changed to weighted minhash, the result is still empty.

import numpy as np
from datasketch import WeightedMinHashGenerator
from datasketch import MinHashLSH,MinHashLSHForest

v1 = np.array([1,2,3]).astype('float')
v2 = np.array([2,2,3]).astype('float')
v3 = np.array([1,2.1,3]).astype('float')

mg = WeightedMinHashGenerator(3, 10)
m1 = mg.minhash(v1)
m2 = mg.minhash(v2)
m3 = mg.minhash(v3)

forest = MinHashLSHForest(num_perm=10)

forest.add("m1", m1)
forest.add("m2", m2)
forest.add("m3", m3)

forest.index()
result = forest.query(m1, 3)
print(result)
ekzhu commented 7 years ago

I see. There is a bug in handling Weighted MinHash in LSH Forest.

It has been fixed. Thanks.

pandasMX commented 7 years ago

@ekzhu Thanks!

pandasMX commented 6 years ago

Hi, after I switch from LSH to LSHforest. Although I set the same num_perm for them, I found the results were still quite different. For example, the top-40 I got from LSHforest is totally different from the sorted top 40 I got from LSH (e.g. I got a list of results from one LSH query, and then compute the weighted jaccard similarity between the query item and each of the result. After that i sort the results ordered from most similar to least similar and take the top 40). Could you check the performance for the LSHforest on weighted minhash? Besides that, if I use MinhashLSH instead, can I control the number of result returned for each query? Because our dataset is very large, the number of result is also very large but we only need the top few similar items. Calculating the distance between the query item and each of the returned result is quite a big cost.

ekzhu commented 6 years ago

Could you create a separate issue for this? Because the original issue has been closed. Thanks!

On Wed, Dec 6, 2017 at 4:26 AM MXCao notifications@github.com wrote:

Hi, after I switch from LSH to LSHforest. Although I set the same num_perm for them, I found the results were still quite different. For example, the top-40 I got from LSHforest is totally different from the sorted top 40 I got from LSH (e.g. I got a list of results from one LSH query, and then compute the weighted jaccard similarity between the query item and each of the result. After that i sort the results ordered from most similar to least similar and take the top 40). Could you check the performance for the LSHforest on weighted minhash? Besides that, if I use MinhashLSH instead, can I control the number of result returned for each query? Because our dataset is very large, the number of result is also very large but we only need the top few similar items. Calculating the distance between the query item and each of the returned result is quite a big cost.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ekzhu/datasketch/issues/35#issuecomment-349582501, or mute the thread https://github.com/notifications/unsubscribe-auth/AATjLpYHYIFe8ulDw0c050DQXcblpu2-ks5s9l2pgaJpZM4QCpPG .