nmslib / hnswlib

Header-only C++/python library for fast approximate nearest neighbors
https://github.com/nmslib/hnswlib
Apache License 2.0
4.34k stars 642 forks source link

How to change a point after building index? #21

Closed universewill closed 6 years ago

universewill commented 6 years ago

I want to change a data point in the index. How can i do that?

import hnswlib
import numpy as np

# Generating sample data
a = np.array([1.0, 0.0, 0.0])
b = np.array([0.0, 1.0, 0.0])
c = np.array([0.0, 0.0, 1.0])
d = np.array([1.0, 1.0, 0.0])

# Declaring index
p = hnswlib.Index(space='l2', dim = 3)

# Initing index - the maximum number of elements should be known beforehand
p.init_index(max_elements = 50, ef_construction = 200, M = 16)

# Element insertion (can be called several times):
p.add_items(a)
p.add_items(b)
p.add_items(c)

labels, distances = p.knn_query(c, k =1)
print labels, distances

#change data points
p.add_items(d, 2)
p.add_items(c, 0)

labels, distances = p.knn_query(d, k =1)
print labels, distances

labels, distances = p.knn_query(c, k =1)
print labels, distances

The query result is not changing after i rewrite the data point label index.

yurymalkov commented 6 years ago

Duplicated by #22