AnnoyIndex(f, metric=‘angular‘)
初始化新的索引树,元素向量维度为f
. Metric 可以是 “angular”, “euclidean”, “manhattan”, “hamming”, or “dot”.a.add_item(i, v)
添加向量元素v
到索引树,其中,i
应该为非负整数a.build(n_trees)
建立索引树,n_trees
表示所建树的个数。树的个数更多,则精度更高,但也要考虑到效率,建立索引树比较耗费时间,一般n_trees=100
均能满足精度要求a.save(fn, prefault=False)
保存模型到磁盘上fn
表示文件路径a.load(fn, prefault=False)
下来模型进行计算,loads (mmaps) an index from disk. If prefault is set to True, it will pre-read the entire file into memory (using mmap with MAP_POPULATE). Default is False.
a.get_nns_by_item(i, n, search_k=-1, include_distances=False)
使用item索引号进行计算,i
表示item索引号, n
表示计算要得到的最近邻的item个数a.get_item_vector(i)
:返回之前添加的item索引号i
所对应的向量a.get_n_items()
返回整个索引树中item的数量a.get_n_trees()
返回整个索引树中树的个数a.get_nns_by_vector(v, n, search_k=-1, include_distances=False)
使用item向量v
进行计算a.on_disk_build(fn)
将索引树建立到具体文件上,这样建立完树后就不用手动保存原文:https://www.cnblogs.com/Cate-Hunter/p/13405941.html