sirrobert-vector-hd
v1.0.0
Published
A high-definition vector library.
Downloads
5
Readme
Overview
A module for working with high-dimensionality vectors (hence "vectors-hd") in JavaScript.
I have successfully used this class in projects requiring:
- vectors with 5,000+ dimensions
- billions of rows of data
- various clustering methods (k-means/medoids, etc.).
Installation
npm install sirrobert-vector-hd
Usage
Creating "degenerate" vectors
Degenerate vectors are vector objects without any dimensionality. They have lots of uses including constructing vectors piecemeal.
var vector = new Vector();
Creating populated vectors
Creating a vector with a pre-existing vector list is easy as well.
var dimensions = {x:238, y:8372, z:18};
var vector = new Vector({coords: dimensions});
Vector inspection
The module provides a set of tools for looking at the vectors. The following list of methods help understand what your vector looks like.
You can directly examine (or manipulate) a dimension this way:
var vector = new Vector({coords: {"x": 2, "my-dim":3}});
vector.coords.x == 2 // true vector.coords['my-dim'] == 4 // true
vector.coords.x = 1238; // set .x to 1238 vector.coords['my-dim'] = 193 // set .my-dim to 193
vector.hasDimension('my-dim') // true vector.hasDimension('fake') // false
Object accessors
get #dimensions
get #distanceMethod
set #distanceMethod
Object methods
#add()
#hasDimensions()
#removeDimension()
#removeDimensions()
#addDimension()
#addDimensions()
#reduceTo()
#reduceBy()
#is()
#toString()
#fromString()
#distanceTo()
#randomize()
Class methods
#distance()
#random()
#mean()
#dimensionalHistorgram()
#addDistanceFunction()
Creating random vectors
Random vectors are useful for things like generating clustering seeds. There are two ways to get a random vector.
In order to generate a random vector dimension, you need to give it a scale. Scales are interpreted as origin-centered. That means if you use a scale of 2000, the new value will be randomly selected between -2000 to +2000.
Randomizing an existing vector (in part or in toto)
Take an existing vector and randomize parts of it.
var dimensions = {x:238, y:8372, z:18};
var vector = new Vector({coords: dimensions});
Now randomize the "x" value.
// The new value of 'x' will be from -1000 to +1000
vector.randomize('x', -1000);
You can also randomize multiple dimensions at once.
// Randomize .y and .z between -1500 to +1500.
vector.randomize(['y','z'], 1500);
Getting a new random vector
You can also generate a new, fully random vector with the class method
#random()
.
// Creates a 4D vector with each value from -2500 to 2500
var vector = Vector.random(['x','y','z','a'], 2500);
TO DO
Unit Tests
Right now the random functions are tested only for range validity (and definiedness). These should be enhanced with a distribution test to ensure that we're getting a good distribution within the permitted range.
Documentation
Obviously I need to finish the documentation!
A note on the module namespace (sirrobert-)
The 'sirrobert-' module prefix is a staging namespace for my modules I am
testing with a small group of people. It will be moved to a general
namespace (for example, the sirrobert-
prefix removed).