我想知道 annoy 库是如何工作的。我从 github 得到了这个测试代码,但我是编码新手,所以我很难理解 .
from annoy import AnnoyIndex
import random
f = 40
t = AnnoyIndex(f, 'angular') #Length of item vector that will be indexed
for i in range(1000):
v = [random.gauss(0, 1) for z in range(f)]
t.add_item(i, v)
t.build(10) # 10 trees
t.save('test.ann')
u = AnnoyIndex(f, 'angular')
u.load('test.ann') # super fast, will just mmap the file
print(u.get_nns_by_item(0, 1000)) # will find the 1000 nearest neighbors
Run Code Online (Sandbox Code Playgroud)