Mmap annoy.AnnoyIndex with examples#
An example showing the AnnoyIndex class.
from __future__ import print_function
import random; random.seed(0)
import time
# from annoy import AnnoyIndex
# from scikitplot.cexternals.annoy import AnnoyIndex
from scikitplot.cexternals.annoy import Index as AnnoyIndex
a = AnnoyIndex(
f=3,
metric='angular',
)
a.add_item(0, [1, 0, 0])
a.add_item(1, [0, 1, 0])
a.add_item(2, [0, 0, 1])
a.build(-1)
a.save('test.annoy')
True
b = AnnoyIndex(
f=3,
metric='angular',
)
b.load('test.annoy')
True
print(b.get_nns_by_item(0, 100))
print(b.get_nns_by_vector([1.0, 0.5, 0.5], 100))
[0, 1, 2]
[0, 1, 2]
Total running time of the script: (0 minutes 0.013 seconds)
Related examples
sphx_glr_auto_examples_annoy_plot_s_compile_cpp.py
Compile and run the C++ Annoy precision example.