18.1.14 • Published 6 years ago

almete.nearestneighborchain v18.1.14

Weekly downloads
7
License
MIT
Repository
github
Last release
6 years ago

almete.NearestNeighborChain

almete.NearestNeighborChain(values, distanceBetween)

Builds a hierarchy of clusters.

argumentdescription
valuesAn array or an object of values to build the hierarchy of clusters from.
distanceBetweenA function to calculate the distance between two values. Value pairs with the lowest distance build a cluster.

Returns clusters as nested arrays. The leaves are either the indexes or the keys of the values, depending on their collection's type.

dependencies

setup

npm

npm install almete.nearestneighborchain

ES module

import NearestNeighborChain from 'almete.nearestneighborchain';

Node

const NearestNeighborChain = require('almete.nearestneighborchain');

browser

<script src="https://unpkg.com/almete.bronkerbosch"></script>
<script src="https://unpkg.com/almete.nearestneighborchain"></script>

The function NearestNeighborChain will be available under the namespace almete.


Include polyfills to support older browsers.

<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>

usage

let object = {a: 4, b: 90, c: 12, d: 61, e: 29};
let clusters = almete.NearestNeighborChain(object, (a, b) => Math.abs(a - b));
// => [['e', ['a', 'c']], ['b', 'd']]

Overlapping clusters are merged together.

let intersection = function(a, b) {
  a = new Set(a), b = new Set(b);
  return [...a].filter(v => b.has(v));
};

let array = ['ac', 'ab', 'baab', 'aba', 'bc'];
let clusters = almete.NearestNeighborChain(array, (a, b) => -intersection(a, b).length);
// => [0, 4, [1, 2, 3]]

see also