From 3bad3b6571cd98f27a6de864bc4e2f0fb2afe671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Wed, 10 Jul 2019 14:05:45 +0200 Subject: [PATCH] fix: improve performance of ward algorithm Use ml-array-median. --- package.json | 1 + src/agnes.js | 25 ++++--------------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index f960212..7abadba 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ }, "dependencies": { "heap": "^0.2.6", + "ml-array-median": "^1.1.1", "ml-distance-euclidean": "^2.0.0", "ml-distance-matrix": "^1.0.0" } diff --git a/src/agnes.js b/src/agnes.js index e2db64f..3988471 100644 --- a/src/agnes.js +++ b/src/agnes.js @@ -1,5 +1,6 @@ import { euclidean } from 'ml-distance-euclidean'; import distanceMatrix from 'ml-distance-matrix'; +import median from 'ml-array-median'; import ClusterLeaf from './ClusterLeaf'; import Cluster from './Cluster'; @@ -90,24 +91,6 @@ function wardLink(cluster1, cluster2, disFun) { ); } -function compareNumbers(a, b) { - return a - b; -} - -function median(values, alreadySorted) { - if (alreadySorted === undefined) alreadySorted = false; - if (!alreadySorted) { - values = [].concat(values).sort(compareNumbers); - } - var l = values.length; - var half = Math.floor(l / 2); - if (l % 2 === 0) { - return (values[half - 1] + values[half]) * 0.5; - } else { - return values[half]; - } -} - /** * Continuously merge nodes that have the least dissimilarity * @param {Array>} distance - Array of points to be clustered @@ -122,7 +105,7 @@ export function agnes(data, options = {}) { const { distanceFunction = euclidean, method = 'single', - isDistanceMatrix = false + isDistanceMatrix = false, } = options; let methodFunc; @@ -203,10 +186,10 @@ export function agnes(data, options = {}) { var count = 0; while (dmin.length > 0) { let aux = dmin.shift(); - const filterInt = function (n) { + const filterInt = (n) => { return aux.indexOf(n) !== -1; }; - const filterDiff = function (n) { + const filterDiff = (n) => { return aux.indexOf(n) === -1; }; for (var q = 0; q < dmin.length; q++) {