Computes pairwise distances between sequences
$ npm install compute-pdist
For use in the browser, use browserify.
var pdist = require( 'compute-pdist' );
This function by default computes distances between pairs of elements of input array X
. It returns an array holding the pairwise distances between the elements of X
.
The function accepts the following options
:
- distance: distance to calculate, either euclidean (default), canberra cosine, chebyshev, manhattan, minkowski
- accessor: accessor function for accessing
array
values. - p: norm order (
p > 0
) ifminkowski distance
is used.
The returned array which holds all the pairwise distances is of length m(m–1)/2
, where m
is the number of elements in X
. It holds the elements of the lower left triangle of the m-by-m distance matrix in column order. The returned array comes with a helper method to extract the (i, j) element of the corresponding distance matrix:
This method returns the distance between the i
th and j
th element of the orignal input X
.
To obtain the full distance matrix, use the squareform
function.
For object arrays
, provide an accessor function
for accessing numeric
values.
var X = [
[1,2],
[2,4],
[3,5]
],
[
[1,1],
[2,2],
[3,7]
]
];
function getValue( d, i ) {
return d[ 1 ];
}
var dist = pdist( X, {
'accessor': getValue
});
// returns 3
The accessor function
is provided two arguments:
- d: current datum.
- i: current datum index.
var pdist = require( 'compute-pdist' );
var X = [
[ 2, 4, 3, 1],
[ 1, 2, 2, 1],
[ 7, 3, 9, 7],
[ 11, 9, 9, 8],
[ 3, 2, 3, 1]
];
var d = pdist( X );
// returns [ 2.449, 9.899, .. ]
// distance between element 0 and 2:
d.get( 0, 2 );
// returns 9.899
// distance matrix:
var dMat = d.toMatrix();
To run the example code from the top-level application directory,
$ node ./examples/index.js
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
Copyright © 2014-2015. The Compute.io Authors.