hamming-lsh v0.0.0
Hamming LSH
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.
Contents
Installation
$ npm install --save hamming-lshUsage
import {Vector as V, Table as T} from 'hamming-lsh';
const t = T(4, 2, 3);
t.add(V([1, 0, 1, 1]));
t.add(V([0, 1, 0, 0]));
t.add(V([0, 1, 1, 0]));
t.query(V([1, 0, 0, 1]));
// => V([1, 0, 1, 1]) with high probabilityAPI
Table
Construct 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 = Table(d, k, l);add
Add a vector v to the lookup table.
Parameters
vVector The vector to add to the lookup table.
Examples
const v = Vector([1, 0, 1, 0]);
t.add(v);query
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 = Vector([0, 1, 0, 1]);
t.query(q);
// => Vector(...)Returns Vector The nearest neighbouring vector if found.
size
Get the number of vectors in the lookup table.
Examples
t.add(Vector(...));
t.size();
// => 1Returns number The number of vectors in the lookup table.
contains
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.
Vector
Construct a vector consisting of binary components where truthy values represent 1 and falsy values represent 0.
Parameters
Examples
const v = Vector([1, 0, 1]);get
Get the component at the specified index of the vector.
Parameters
inumber The index of the component to get.
Examples
const v = Vector([1, 0, 1, 1]);
v.get(0);
// => 1Returns number The component at the index if found.
size
Get the number of components in the vector.
Examples
const v = Vector([1, 0, 0, 1]);
v.size();
// => 4Returns number The number of components in the vector.
License
Copyright © 2016 Kasper Kronborg Isager. Licensed under the terms of the MIT license.
9 years ago