The package is one implementation of paper Locality-Sensitive Hashing Scheme Based on p-Stable Distributions in SCG’2014. P-stable-lsh a novel Locality-Sensitive Hashing scheme for the Approximate Nearest Neighbor Problem under
Note: This code is used as the practice of the paper, and there are few optimizations. Sharing is for communication and learning. If it is a high-performance scenario, please optimize as appropriate.
pip install p-stable-lsh-pythonThe following example shows all features of the package, If you want to know the details, please refer to the source code and comments.
import numpy as np
import p_stable_lsh.pstable as psl
dim = 100 # vector dimension
data = [np.random.random(dim) for _ in range(2)] # generate two vectors
r = 50.0 # the parameter $r$ in paper
m1 = psl.pstable(r, dim, metric_dim=1)
m1.lsh(data[0])
m2 = psl.pstable(r, dim, metric_dim=1)
m2.lsh(data[1])
print(m1.md(m2)) # estimate value
print(m1.p(np.average(sum(np.abs(data[0]-data[1]))))) # theoretical(true) value
m1 = psl.pstable(r, dim, metric_dim=2)
m1.lsh(data[0])
m2 = psl.pstable(r, dim, metric_dim=2)
m2.lsh(data[1])
print(m1.md(m2)) # estimate value
print(m1.p(np.sqrt(sum([i**2 for i in data[0]-data[1]])))) # theoretical(true) valueDefine the parameter
import numpy as np
import p_stable_lsh.pstable as psl
r = 50.0 # the parameter $r$ in paper
dim = 100 # vector dimension
data = [np.random.random(dim) for _ in range(3)] # generate two vectorsInstantiate pstable object with specific dimension space. (
m1 = psl.pstable(r, dim, metric_dim=1)
m2 = psl.pstable(r, dim, metric_dim=1)Hash vectors with p-stable LSH function.
m1.lsh(data[0])
m2.lsh(data[1])Estimate distance between two object.
m1.md(m2) # estimate valueShow the ground truth distance probability using integration shown in paper.
l1_distance = np.average(sum(np.abs(data[0]-data[1])))
m1.p(l1_distance)Another way to instantiate pstable object with hash values.
m3 = psl.pstable(r, dim, metric_dim=1, hashvalues=m1.hashvalues)
m3.md(m2)Update object hash values with different vector.
m2.lsh(data[2])
m3.md(m2)The parameter .md method). The following shows different probability results varied by
Data points code
import p_stable_lsh.pstable as psl
r_list = [10, 30, 50, 100, 300, ]
result = []
for r in r_list:
tmp = []
m = psl.pstable(r, dim, metric_dim=1)
for i in range(1, 800):
tmp.append(m.p(i))
result.append(tmp)
