An implementation of locality-sensitive hashing for Hamming space
Locality-sensitive hashing (abbreviated LSH) is a method often used for answering approximate nearest neighbour queries in high-dimensional data sets. This library implements a version of LSH for solving the approximate nearest neighbour problem for binary vectors in Hamming space.
$ npm install --save hamming-lshimport {Vector, Table} from 'hamming-lsh';
const t = new Table(4, 2, 3);
t.add(new Vector([1, 0, 1, 1]));
t.add(new Vector([0, 1, 0, 0]));
t.add(new Vector([0, 1, 1, 0]));
t.query(new Vector([1, 0, 0, 1]));
// => Vector [1, 0, 1, 1] with high probabilityConstruct a lookup table for vectors of dimensionality d where vectors are hashed using k-width hash values
(random vector projections) into l sets of hashes.
Parameters
dnumber The number of dimensions of vectors in the table.knumber The width of each vector hash.lnumber The number of hash sets to use.
Examples
const d = 4;
const k = 2;
const l = 2;
const t = new Table(d, k, l);Add a vector v to the lookup table.
Parameters
vVector The vector to add to the lookup table.
Examples
const v = new Vector([1, 0, 1, 0]);
t.add(v);Query the lookup table for the nearest neighbour of a query vector q.
Parameters
qVector The query vector to look up the nearest neighbour of.
Examples
const q = new Vector([0, 1, 0, 1]);
t.query(q);
// => Vector(...)Returns Vector The nearest neighbouring vector if found.
Get the number of vectors in the lookup table.
Examples
t.add(new Vector(...));
t.size();
// => 1Returns number The number of vectors in the lookup table.
Check if the lookup table contains a specific vector.
Parameters
vVector The vector to check for.
Returns boolean true if the table contains the vector, otherwise false.
Construct a vector consisting of binary components where truthy values represent 1 and falsy values represent 0.
Parameters
Examples
const v = new Vector([1, 0, 1]);Get the component at the specified index of the vector.
Parameters
inumber The index of the component to get.
Examples
const v = new Vector([1, 0, 1, 1]);
v.get(0);
// => 1Returns number The component at the index if found.
Get the number of components in the vector.
Examples
const v = new Vector([1, 0, 0, 1]);
v.size();
// => 4Returns number The number of components in the vector.
Copyright © 2016-2018 Kasper Kronborg Isager. Licensed under the terms of the MIT license.