[Pkg-javascript-commits] [science.js] 23/87: Clean up stats.hcluster.
bhuvan krishna
bhuvan-guest at moszumanska.debian.org
Thu Dec 8 06:11:54 UTC 2016
This is an automated email from the git hooks/post-receive script.
bhuvan-guest pushed a commit to branch master
in repository science.js.
commit 1602c8ec3d641e5dd10e7a3fb449df111f66ac3d
Author: Jason Davies <jason at jasondavies.com>
Date: Thu Aug 25 20:48:29 2011 +0100
Clean up stats.hcluster.
---
science.stats.js | 101 ++++++++++++++++++++++++++++++--------------------
science.stats.min.js | 2 +-
src/stats/hcluster.js | 99 ++++++++++++++++++++++++++++++-------------------
3 files changed, 122 insertions(+), 80 deletions(-)
diff --git a/science.stats.js b/science.stats.js
index 4411920..3b6e00e 100644
--- a/science.stats.js
+++ b/science.stats.js
@@ -252,80 +252,98 @@ function science_stats_kmeansRandom(k, vectors) {
}
science.stats.hcluster = function() {
var distance = science.stats.distance.euclidean,
- linkage = "";
+ linkage = "simple"; // simple, complete or average
function hcluster(vectors) {
- var n = vectors.length;
- var dMin = [];
- var cSize = [];
- var distMatrix = [];
- var clusters = [];
-
- var c1, c2, c1Cluster, c2Cluster, p, root , newCentroid;
-
- var i,
+ var n = vectors.length,
+ dMin = [],
+ cSize = [],
+ distMatrix = [],
+ clusters = [],
+ c1,
+ c2,
+ c1Cluster,
+ c2Cluster,
+ p,
+ root,
+ i,
j;
- // Initialize distance matrix and vector of closest clusters
+ // Initialise distance matrix and vector of closest clusters.
i = -1; while (++i < n) {
dMin[i] = 0;
distMatrix[i] = [];
j = -1; while (++j < n) {
distMatrix[i][j] = i === j ? Infinity : distance(vectors[i] , vectors[j]);
- if (distMatrix[i][dMin[i]] > distMatrix[i][j]) dMin[i] = j ;
+ if (distMatrix[i][dMin[i]] > distMatrix[i][j]) dMin[i] = j;
}
}
// create leaves of the tree
i = -1; while (++i < n) {
clusters[i] = [];
- clusters[i][0] = {left: null, right: null, dist: 0, centroid: vectors[i], size: 1, depth: 0};
+ clusters[i][0] = {
+ left: null,
+ right: null,
+ dist: 0,
+ centroid: vectors[i],
+ size: 1,
+ depth: 0
+ };
cSize[i] = 1;
}
// Main loop
for (p = 0; p < n-1; p++) {
// find the closest pair of clusters
- c1 = 0 ;
- for (i = 0 ; i < n ; i++) {
+ c1 = 0;
+ for (i = 0; i < n; i++) {
if (distMatrix[i][dMin[i]] < distMatrix[c1][dMin[c1]]) c1 = i;
}
c2 = dMin[c1];
// create node to store cluster info
- c1Cluster = clusters[c1][0] ;
- c2Cluster = clusters[c2][0] ;
-
- newCentroid = calculateCentroid(c1Cluster.size, c1Cluster.centroid, c2Cluster.size, c2Cluster.centroid) ;
- newCluster = {left: c1Cluster, right: c2Cluster, dist: distMatrix[c1][c2], centroid: newCentroid, size: c1Cluster.size + c2Cluster.size, depth: 1 + Math.max(c1Cluster.depth, c2Cluster.depth)};
+ c1Cluster = clusters[c1][0];
+ c2Cluster = clusters[c2][0];
+
+ newCluster = {
+ left: c1Cluster,
+ right: c2Cluster,
+ dist: distMatrix[c1][c2],
+ centroid: calculateCentroid(c1Cluster.size, c1Cluster.centroid,
+ c2Cluster.size, c2Cluster.centroid),
+ size: c1Cluster.size + c2Cluster.size,
+ depth: 1 + Math.max(c1Cluster.depth, c2Cluster.depth)
+ };
clusters[c1].splice(0, 0, newCluster);
cSize[c1] += cSize[c2];
// overwrite row c1 with respect to the linkage type
- for (j = 0 ; j < n ; j++) {
- if (linkage == "single") {
- if (distMatrix[c1][j] > distMatrix[c2][j])
- distMatrix[j][c1] = distMatrix[c1][j] = distMatrix[c2][j] ;
- } else if (linkage == "complete") {
- if (distMatrix[c1][j] < distMatrix[c2][j])
- distMatrix[j][c1] = distMatrix[c1][j] = distMatrix[c2][j] ;
- } else if (linkage == "average") {
- var avg = ( cSize[c1] * distMatrix[c1][j] + cSize[c2] * distMatrix[c2][j]) / (cSize[c1] + cSize[j])
- distMatrix[j][c1] = distMatrix[c1][j] = avg ;
- }
+ for (j = 0; j < n; j++) {
+ switch (linkage) {
+ case "single":
+ if (distMatrix[c1][j] > distMatrix[c2][j])
+ distMatrix[j][c1] = distMatrix[c1][j] = distMatrix[c2][j];
+ break;
+ case "complete":
+ if (distMatrix[c1][j] < distMatrix[c2][j])
+ distMatrix[j][c1] = distMatrix[c1][j] = distMatrix[c2][j];
+ break;
+ case "average":
+ distMatrix[j][c1] = distMatrix[c1][j] = (cSize[c1] * distMatrix[c1][j] + cSize[c2] * distMatrix[c2][j]) / (cSize[c1] + cSize[j]);
+ break;
+ }
}
distMatrix[c1][c1] = Infinity;
// infinity out old row c2 and column c2
- for (i = 0 ; i < n ; i++)
+ for (i = 0; i < n; i++)
distMatrix[i][c2] = distMatrix[c2][i] = Infinity;
// update dmin and replace ones that previous pointed to c2 to point to c1
- for (j = 0; j < n ; j++) {
- if (dMin[j] == c2)
- dMin[j] = c1;
- if (distMatrix[c1][j] < distMatrix[c1][dMin[c1]])
- dMin[c1] = j;
+ for (j = 0; j < n; j++) {
+ if (dMin[j] == c2) dMin[j] = c1;
+ if (distMatrix[c1][j] < distMatrix[c1][dMin[c1]]) dMin[c1] = j;
}
// keep track of the last added cluster
@@ -339,10 +357,13 @@ science.stats.hcluster = function() {
};
function calculateCentroid(c1Size, c1Centroid, c2Size, c2Centroid) {
- var newCentroid = [];
- var newSize = c1Size + c2Size;
- for (var i = 0; i < c1Centroid.length; i++)
+ var newCentroid = [],
+ newSize = c1Size + c2Size,
+ n = c1Centroid.length,
+ i = -1;
+ while (++i < n) {
newCentroid[i] = (c1Size * c1Centroid[i] + c2Size * c2Centroid[i]) / newSize;
+ }
return newCentroid;
}
science.stats.iqr = function(x) {
diff --git a/science.stats.min.js b/science.stats.min.js
index 84c8fb5..5f9687f 100644
--- a/science.stats.min.js
+++ b/science.stats.min.js
@@ -1 +1 @@
-(function(){function h(a,b){var c=b+1;while(c<a.length&&a[c]===0)c++;return c}function g(a,b,c,d){var e=d[0],f=d[1],g=h(b,f);if(g<a.length&&a[g]-a[c]<a[c]-a[e]){var i=h(b,e);d[0]=i,d[1]=g}}function f(a){return(a=1-a*a*a)*a*a}function e(a){var b=a.length,c=0;while(++c<b)if(a[c-1]>=a[c])return!1;return!0}function d(a){var b=a.length,c=-1;while(++c<b)if(!isFinite(a[c]))return!1;return!0}function c(a,b,c,d){var e=[],f=a+c;for(var g=0;g<b.length;g++)e[g]=(a*b[g]+c*d[g])/f;return e}function b( [...]
\ No newline at end of file
+(function(){function h(a,b){var c=b+1;while(c<a.length&&a[c]===0)c++;return c}function g(a,b,c,d){var e=d[0],f=d[1],g=h(b,f);if(g<a.length&&a[g]-a[c]<a[c]-a[e]){var i=h(b,e);d[0]=i,d[1]=g}}function f(a){return(a=1-a*a*a)*a*a}function e(a){var b=a.length,c=0;while(++c<b)if(a[c-1]>=a[c])return!1;return!0}function d(a){var b=a.length,c=-1;while(++c<b)if(!isFinite(a[c]))return!1;return!0}function c(a,b,c,d){var e=[],f=a+c,g=b.length,h=-1;while(++h<g)e[h]=(a*b[h]+c*d[h])/f;return e}function b [...]
\ No newline at end of file
diff --git a/src/stats/hcluster.js b/src/stats/hcluster.js
index cb0ae2a..a28f9ce 100644
--- a/src/stats/hcluster.js
+++ b/src/stats/hcluster.js
@@ -1,79 +1,97 @@
science.stats.hcluster = function() {
var distance = science.stats.distance.euclidean,
- linkage = "";
+ linkage = "simple"; // simple, complete or average
function hcluster(vectors) {
- var n = vectors.length;
- var dMin = [];
- var cSize = [];
- var distMatrix = [];
- var clusters = [];
-
- var c1, c2, c1Cluster, c2Cluster, p, root , newCentroid;
-
- var i,
+ var n = vectors.length,
+ dMin = [],
+ cSize = [],
+ distMatrix = [],
+ clusters = [],
+ c1,
+ c2,
+ c1Cluster,
+ c2Cluster,
+ p,
+ root,
+ i,
j;
- // Initialize distance matrix and vector of closest clusters
+ // Initialise distance matrix and vector of closest clusters.
i = -1; while (++i < n) {
dMin[i] = 0;
distMatrix[i] = [];
j = -1; while (++j < n) {
distMatrix[i][j] = i === j ? Infinity : distance(vectors[i] , vectors[j]);
- if (distMatrix[i][dMin[i]] > distMatrix[i][j]) dMin[i] = j ;
+ if (distMatrix[i][dMin[i]] > distMatrix[i][j]) dMin[i] = j;
}
}
// create leaves of the tree
i = -1; while (++i < n) {
clusters[i] = [];
- clusters[i][0] = {left: null, right: null, dist: 0, centroid: vectors[i], size: 1, depth: 0};
+ clusters[i][0] = {
+ left: null,
+ right: null,
+ dist: 0,
+ centroid: vectors[i],
+ size: 1,
+ depth: 0
+ };
cSize[i] = 1;
}
// Main loop
for (p = 0; p < n-1; p++) {
// find the closest pair of clusters
- c1 = 0 ;
- for (i = 0 ; i < n ; i++) {
+ c1 = 0;
+ for (i = 0; i < n; i++) {
if (distMatrix[i][dMin[i]] < distMatrix[c1][dMin[c1]]) c1 = i;
}
c2 = dMin[c1];
// create node to store cluster info
- c1Cluster = clusters[c1][0] ;
- c2Cluster = clusters[c2][0] ;
+ c1Cluster = clusters[c1][0];
+ c2Cluster = clusters[c2][0];
- newCentroid = calculateCentroid(c1Cluster.size, c1Cluster.centroid, c2Cluster.size, c2Cluster.centroid) ;
- newCluster = {left: c1Cluster, right: c2Cluster, dist: distMatrix[c1][c2], centroid: newCentroid, size: c1Cluster.size + c2Cluster.size, depth: 1 + Math.max(c1Cluster.depth, c2Cluster.depth)};
+ newCluster = {
+ left: c1Cluster,
+ right: c2Cluster,
+ dist: distMatrix[c1][c2],
+ centroid: calculateCentroid(c1Cluster.size, c1Cluster.centroid,
+ c2Cluster.size, c2Cluster.centroid),
+ size: c1Cluster.size + c2Cluster.size,
+ depth: 1 + Math.max(c1Cluster.depth, c2Cluster.depth)
+ };
clusters[c1].splice(0, 0, newCluster);
cSize[c1] += cSize[c2];
// overwrite row c1 with respect to the linkage type
- for (j = 0 ; j < n ; j++) {
- if (linkage == "single") {
- if (distMatrix[c1][j] > distMatrix[c2][j])
- distMatrix[j][c1] = distMatrix[c1][j] = distMatrix[c2][j] ;
- } else if (linkage == "complete") {
- if (distMatrix[c1][j] < distMatrix[c2][j])
- distMatrix[j][c1] = distMatrix[c1][j] = distMatrix[c2][j] ;
- } else if (linkage == "average") {
- var avg = ( cSize[c1] * distMatrix[c1][j] + cSize[c2] * distMatrix[c2][j]) / (cSize[c1] + cSize[j])
- distMatrix[j][c1] = distMatrix[c1][j] = avg ;
- }
+ for (j = 0; j < n; j++) {
+ switch (linkage) {
+ case "single":
+ if (distMatrix[c1][j] > distMatrix[c2][j])
+ distMatrix[j][c1] = distMatrix[c1][j] = distMatrix[c2][j];
+ break;
+ case "complete":
+ if (distMatrix[c1][j] < distMatrix[c2][j])
+ distMatrix[j][c1] = distMatrix[c1][j] = distMatrix[c2][j];
+ break;
+ case "average":
+ distMatrix[j][c1] = distMatrix[c1][j] = (cSize[c1] * distMatrix[c1][j] + cSize[c2] * distMatrix[c2][j]) / (cSize[c1] + cSize[j]);
+ break;
+ }
}
distMatrix[c1][c1] = Infinity;
// infinity out old row c2 and column c2
- for (i = 0 ; i < n ; i++)
+ for (i = 0; i < n; i++)
distMatrix[i][c2] = distMatrix[c2][i] = Infinity;
// update dmin and replace ones that previous pointed to c2 to point to c1
- for (j = 0; j < n ; j++) {
- if (dMin[j] == c2)
- dMin[j] = c1;
- if (distMatrix[c1][j] < distMatrix[c1][dMin[c1]])
- dMin[c1] = j;
+ for (j = 0; j < n; j++) {
+ if (dMin[j] == c2) dMin[j] = c1;
+ if (distMatrix[c1][j] < distMatrix[c1][dMin[c1]]) dMin[c1] = j;
}
// keep track of the last added cluster
@@ -87,9 +105,12 @@ science.stats.hcluster = function() {
};
function calculateCentroid(c1Size, c1Centroid, c2Size, c2Centroid) {
- var newCentroid = [];
- var newSize = c1Size + c2Size;
- for (var i = 0; i < c1Centroid.length; i++)
+ var newCentroid = [],
+ newSize = c1Size + c2Size,
+ n = c1Centroid.length,
+ i = -1;
+ while (++i < n) {
newCentroid[i] = (c1Size * c1Centroid[i] + c2Size * c2Centroid[i]) / newSize;
+ }
return newCentroid;
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/science.js.git
More information about the Pkg-javascript-commits
mailing list