Simple annoy.AnnoyIndex with examples#
An example showing the AnnoyIndex class.
from annoy import AnnoyIndex from scikitplot.annoy import AnnoyIndex
from scikitplot.annoy import Index as AnnoyIndex
idx = AnnoyIndex(
f=3,
metric='angular',
)
idx.add_item(0, [1, 0, 0])
idx.add_item(1, [0, 1, 0])
idx.add_item(2, [0, 0, 1])
idx.build(-1)
print(idx.get_nns_by_item(0, 100))
print(idx.get_nns_by_vector([1.0, 0.5, 0.5], 100))
idx.info()
[0, 1, 2]
[0, 1, 2]
{'f': 3, 'metric': 'angular', 'n_neighbors': 5, 'on_disk_path': None, 'prefault': False, 'seed': None, 'verbose': None, 'schema_version': 0, 'n_items': 3, 'n_trees': 3, 'memory_usage_byte': 272, 'memory_usage_mib': 0.0002593994140625}
def plot(idx, y=None, **kwargs):
import numpy as np
import matplotlib.pyplot as plt
import scikitplot.cexternals._annoy._plotting as utils
single = np.zeros(idx.get_n_items(), dtype=int)
if y is None:
double = np.random.uniform(0, 1, idx.get_n_items()).round()
# single vs double
fig, ax = plt.subplots(ncols=2, figsize=(12, 5))
alpha = kwargs.pop("alpha", 0.8)
y2 = utils.plot_annoy_index(
idx,
dims = list(range(idx.f)),
plot_kwargs={"draw_legend": False},
ax=ax[0],
)[0]
utils.plot_annoy_knn_edges(
idx,
y2,
k=1,
line_kwargs={"alpha": alpha},
ax=ax[1],
)
# idx.unbuild()
# idx.build(10)
plot(idx)
Total running time of the script: (0 minutes 0.055 seconds)
Related examples