Locality-Sensitive Hashing implementation for indexing vectors using random projections
npm install lsh-indexA TypeScript implementation of Locality-Sensitive Hashing for indexing similar items using random buckets.
- Random Projection for Locality Sensitive Hashing
Install the package using npm.
``bash`
npm install lsh-index
Import and use the LSH class with your desired configuration:
`typescript
import { LSH } from "lsh-index";
const lsh = new LSH({
dimensions: 3,
numProjections: 10,
numBands: 5,
bucketSize: 4
});
// Insert vectors
lsh.insert({ id: "point1", vector: [1, 2, 3] });
lsh.insert({ id: "point2", vector: [1.1, 2.1, 3.1] });
// Query similar vectors
const results = lsh.query({ vector: [1, 2, 3], maxDistance: 0.5 });
`
Creates a new LSH instance for similarity search.
#### options
- dimensions (number): Number of dimensions in your input vectorsnumProjections
- (number): Number of random projections to use. Must be a multiple of numBandsnumBands
- (number): Number of bands for LSH bucketingbucketSize
- (number, optional): Size of each bucket for quantization (default: 4)distanceMetric
- (function, optional): Custom distance metric function (default: Euclidean)
> Note: The numProjections must be a multiple of numBands to ensure even distribution of projections across bands. For example, if you have 5 bands, valid values for numProjections would be 5, 10, 15, etc.
#### insert(params)
Inserts a vector into the LSH index.
- params.id (string): Unique identifier for the vectorparams.vector
- (number[]): Vector to insert
#### query(params)
Finds similar vectors within the specified distance.
- params.vector (number[]): Query vectorparams.maxDistance` (number): Maximum distance threshold
-
- Returns: Array of IDs of similar vectors
#### clear()
Removes all vectors from the index.
#### export()
Exports the current state of the LSH index.
MIT