aparrish / simpleneighbors

A clean and easy interface for performing nearest-neighbor lookups
BSD 3-Clause "New" or "Revised" License
50 stars 1 forks source link

how to get the similarity score #2

Closed Cumberbatch08 closed 4 years ago

Cumberbatch08 commented 4 years ago

after I build the tree model, and I use the method nearest(vec, n) to get the nearest item in the candidate corpus, but I want get the similarity score. becase, some returned results are not very near. If I have the score of every returned result, I can filter the result. Is ther any method can do this?

aparrish commented 4 years ago

The easiest way to do this is with the dist() method, which returns the distance between two items.

You can also (unofficially) call the underlying Annoy get_nns_by_item() method and set the include_distances parameter to True, which will return a list of 2-tuples with the index and the distance for each match:

nn = SimpleNeighbors(...)
# add a bunch of stuff to the index
results = nn.annoy.get_nns_by_item(idx, n, include_distances=True)

(Though in that case, you'll have to do the work of looking up the index of the item you want to look up and the value of the item at the indices returned from get_nns_by_item() yourself.)