蟒
ngt的Python项目详细描述
python ngt
安装
python绑定pybind11(ngtpy)安装如下。
pip3 install ngt
如果您想将python绑定与cTypes(ngt)一起使用,还必须根据README安装ngt库。
您可以从源代码安装python绑定,其中包含pybind11和cTypes。在安装python绑定之前,必须根据README安装ngt库,如下所示。
pip3 install pybind11
pip3 install numpy
cd NGT_ROOT/python
python3 setup.py sdist
pip3 install dist/ngt-x.x.x.tar.gz
请注意,来自pypi的ngtpy包的搜索速度比在您的计算机上构建的ngtpy包的搜索速度慢,因此包可以在较旧的cpu上运行。
文档
简单示例
ngtpy(pybind11)
与ngt(cTypes)相比,ngtpy(pybind11)可以减少处理时间。特别是在搜索时间短的情况下更有效。
importngtpyimportrandomdim=10objects=[]foriinrange(0,100):vector=random.sample(range(100),dim)objects.append(vector)query=objects[0]ngtpy.create(b"tmp",dim)index=ngtpy.Index(b"tmp")index.batch_insert(objects)index.save()result=index.search(query,3)fori,oinenumerate(result):print(str(i)+": "+str(o[0])+", "+str(o[1]))object=index.get_object(o[0])print(object)
另请参见sample.py。
NGT(cTypes)
fromngtimportbaseasngtimportrandomdim=10objects=[]foriinrange(0,100):vector=random.sample(range(100),dim)objects.append(vector)query=objects[0]index=ngt.Index.create(b"tmp",dim)index.insert(objects)# You can also insert objects from a file like this.# index.insert_from_tsv('list.tsv') index.save()# You can load saved the index like this.# index = ngt.Index(b"tmp")result=index.search(query,3)fori,oinenumerate(result):print(str(i)+": "+str(o.id)+", "+str(o.distance))object=index.get_object(o.id)print(object)