Package Exports
- fast-cosine-similarity
- fast-cosine-similarity/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fast-cosine-similarity) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
fast-cosine-similarity
Compute the cosine-similarity of two vectors. Super simple and fast implementation.
- Up to 6x faster than the
compute-cosine-similaritypackage from simple testing of 40k vectors to a query vector. - Full typescript support.
- Incredibly small package size.
- No external dependencies
Installation
npm:
npm install fast-cosine-similarityyarn:
yarn add fast-cosine-similarityHow to use
import { cosineSimilarity } from "fast-cosine-similarity";
const vector1 = [0.2, 0.5, 0.4, 0.1, 0.7];
const vector2 = [0.1, 0.6, 0.3, 0.2, 0.8];
const similarity = cosineSimilarity(vector1, vector2);Important things to know
- Will not work if any of the vectors are zero vectors (regardless of length).
- Different length vectors are supported. The shorter vector will be padded with zeros.
- All elements of the vectors must be numbers.
- The vectors must not be empty.
Errors
The following errors might be thrown when using the package: All error classes are exported from the package.
EmptyVectorError
Thrown when one of the vectors is empty.
InvalidVectorTypeError
Thrown when any of the vectors contains elements that are not numbers. All elements of both arrays must be numbers.
MissingVectorError
Thrown when one of the vectors parameters is falsy (null, undefined). Both parameters must be arrays of numbers.
InvalidParameterType
Thrown when either of the parameters is not an array. Both parameters must be arrays of numbers.
ZeroVectorError
Thrown when one of the vectors is a zero vector. All the elements of a vector must not be zero.