Comparing version 0.5.2 to 0.6.0
@@ -57,6 +57,6 @@ "use strict"; | ||
*/ | ||
function init(len, val) { | ||
var v = []; | ||
function init(len, val, v) { | ||
v = v || []; | ||
for (var i = 0; i < len; i++) { | ||
v.push(val); | ||
v[i] = val; | ||
}return v; | ||
@@ -66,4 +66,2 @@ } | ||
function skmeans(data, k, initial, maxit) { | ||
"use strict"; | ||
var ks = [], | ||
@@ -74,3 +72,4 @@ idxs = [], | ||
it = maxit || MAX; | ||
var multi = data[0].length; | ||
var vlen = data[0].length, | ||
multi = vlen > 0; | ||
@@ -109,3 +108,3 @@ if (!initial) { | ||
count[_j] = 0; | ||
sum[_j] = multi ? init(multi, 0) : 0; | ||
sum[_j] = multi ? init(vlen, 0, sum[_j]) : 0; | ||
old[_j] = ks[_j]; | ||
@@ -120,14 +119,31 @@ } | ||
for (var _i2 = 0; _i2 < len; _i2++) { | ||
for (var h = 0; h < multi; h++) { | ||
sum[idxs[_i2]][h] += data[_i2][h]; | ||
var _idx = idxs[_i2], | ||
vsum = sum[_idx], | ||
vect = data[_i2]; | ||
for (var h = 0; h < vlen; h++) { | ||
vsum[h] += vect[h]; | ||
} | ||
count[idxs[_i2]]++; | ||
count[_idx]++; | ||
} | ||
// Calculate de average for each centroid | ||
// and de distance between old and new centroids | ||
conv = true; | ||
for (var _j3 = 0; _j3 < k; _j3++) { | ||
for (var _h = 0; _h < multi; _h++) { | ||
ks[_j3][_h] = sum[_j3][_h] / count[_j3] || 0; | ||
dif += old[_j3][_h] - ks[_j3][_h]; | ||
var ksj = ks[_j3], | ||
sumj = sum[_j3], | ||
oldj = old[_j3], | ||
cj = count[_j3]; | ||
// New average | ||
for (var _h = 0; _h < vlen; _h++) { | ||
ksj[_h] = sumj[_h] / cj || 0; | ||
} | ||
// Find if centroids have moved | ||
if (conv) { | ||
for (var _h2 = 0; _h2 < vlen; _h2++) { | ||
if (oldj[_h2] != ksj[_h2]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
@@ -146,7 +162,14 @@ } | ||
ks[_j4] = sum[_j4] / count[_j4] || 0; | ||
dif += old[_j4] - ks[_j4]; | ||
} | ||
// Find if centroids have moved | ||
conv = true; | ||
for (var _j5 = 0; _j5 < k; _j5++) { | ||
if (old[_j5] != ks[_j5]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
conv = dif === 0 || --it <= 0; | ||
conv = conv || --it <= 0; | ||
} while (!conv); | ||
@@ -153,0 +176,0 @@ |
@@ -1,3 +0,3 @@ | ||
/*! skmeans 2017-06-08 */ | ||
/*! skmeans 2017-06-18 */ | ||
"use strict";!function r(n,o,t){function e(a,i){if(!o[a]){if(!n[a]){var u="function"==typeof require&&require;if(!i&&u)return u(a,!0);if(f)return f(a,!0);var v=new Error("Cannot find module '"+a+"'");throw v.code="MODULE_NOT_FOUND",v}var s=o[a]={exports:{}};n[a][0].call(s.exports,function(r){var o=n[a][1][r];return e(o||r)},s,s.exports,r,n,o,t)}return o[a].exports}for(var f="function"==typeof require&&require,a=0;a<t.length;a++)e(t[a]);return e}({1:[function(r,n,o){!function(n){var o=r("./main.js");n.skmeans=o}(window)},{"./main.js":2}],2:[function(r,n,o){function t(r,n){for(var o=r.length,t=0,e=0;e<o;e++){var f=(r[e]||0)-(n[e]||0);t+=f*f}return Math.sqrt(t)}function e(r,n){for(var o=[],t=0;t<r;t++)o.push(n);return o}function f(r,n,o,f){var i=[],u=[],v=r.length,s=!1,c=f||a,h=r[0].length;if(o)i=o;else for(var l=0;l<n;l++)i.push(r[Math.floor(Math.random()*v)]);do{for(var p=0;p<v;p++){for(var d=1/0,x=0,m=0;m<n;m++){var q=h?t(r[p],i[m]):Math.abs(r[p]-i[m]);q<d&&(d=q,x=m)}u[p]=x}for(var w=[],M=[],g=[],O=0,j=0;j<n;j++)w[j]=0,M[j]=h?e(h,0):0,g[j]=i[j];if(h){for(var k=0;k<n;k++)i[k]=[];for(var y=0;y<v;y++){for(var D=0;D<h;D++)M[u[y]][D]+=r[y][D];w[u[y]]++}for(var E=0;E<n;E++)for(var N=0;N<h;N++)i[E][N]=M[E][N]/w[E]||0,O+=g[E][N]-i[E][N]}else{for(var U=0;U<v;U++)M[u[U]]+=r[U],w[u[U]]++;for(var _=0;_<n;_++)i[_]=M[_]/w[_]||0,O+=g[_]-i[_]}s=0===O||--c<=0}while(!s);return{it:a-c,k:n,idxs:u,centroids:i}}var a=1e4;n.exports=f},{}]},{},[1]); | ||
"use strict";!function r(o,n,e){function t(a,i){if(!n[a]){if(!o[a]){var u="function"==typeof require&&require;if(!i&&u)return u(a,!0);if(f)return f(a,!0);var v=new Error("Cannot find module '"+a+"'");throw v.code="MODULE_NOT_FOUND",v}var s=n[a]={exports:{}};o[a][0].call(s.exports,function(r){var n=o[a][1][r];return t(n||r)},s,s.exports,r,o,n,e)}return n[a].exports}for(var f="function"==typeof require&&require,a=0;a<e.length;a++)t(e[a]);return t}({1:[function(r,o,n){!function(o){var n=r("./main.js");o.skmeans=n}(window)},{"./main.js":2}],2:[function(r,o,n){function e(r,o){for(var n=r.length,e=0,t=0;t<n;t++){var f=(r[t]||0)-(o[t]||0);e+=f*f}return Math.sqrt(e)}function t(r,o,n){n=n||[];for(var e=0;e<r;e++)n[e]=o;return n}var f=1e4;o.exports=function(r,o,n,a){var i=[],u=[],v=r.length,s=!1,c=a||f,h=r[0].length,l=h>0;if(n)i=n;else for(var d=0;d<o;d++)i.push(r[Math.floor(Math.random()*v)]);do{for(var p=0;p<v;p++){for(var x=1/0,m=0,q=0;q<o;q++){var w=l?e(r[p],i[q]):Math.abs(r[p]-i[q]);w<x&&(x=w,m=q)}u[p]=m}for(var M=[],g=[],k=[],b=0;b<o;b++)M[b]=0,g[b]=l?t(h,0,g[b]):0,k[b]=i[b];if(l){for(var O=0;O<o;O++)i[O]=[];for(var j=0;j<v;j++){for(var y=u[j],D=g[y],E=r[j],N=0;N<h;N++)D[N]+=E[N];M[y]++}s=!0;for(var U=0;U<o;U++){for(var _=i[U],C=g[U],F=k[U],L=M[U],T=0;T<h;T++)_[T]=C[T]/L||0;if(s)for(var z=0;z<h;z++)if(F[z]!=_[z]){s=!1;break}}}else{for(var A=0;A<v;A++)g[u[A]]+=r[A],M[u[A]]++;for(var B=0;B<o;B++)i[B]=g[B]/M[B]||0;s=!0;for(var G=0;G<o;G++)if(k[G]!=i[G]){s=!1;break}}s=s||--c<=0}while(!s);return{it:f-c,k:o,idxs:u,centroids:i}}},{}]},{},[1]); |
45
main.js
@@ -35,5 +35,5 @@ /*jshint esversion: 6 */ | ||
*/ | ||
function init(len,val) { | ||
var v = []; | ||
for(let i=0;i<len;i++) v.push(val); | ||
function init(len,val,v) { | ||
v = v || []; | ||
for(let i=0;i<len;i++) v[i] = val; | ||
return v; | ||
@@ -45,3 +45,3 @@ } | ||
var conv = false, it = maxit || MAX; | ||
var multi = data[0].length; | ||
var vlen = data[0].length, multi = vlen>0; | ||
@@ -77,3 +77,3 @@ if(!initial) { | ||
count[j] = 0; | ||
sum[j] = multi? init(multi,0) : 0; | ||
sum[j] = multi? init(vlen,0,sum[j]) : 0; | ||
old[j] = ks[j]; | ||
@@ -88,14 +88,26 @@ } | ||
for(let i=0;i<len;i++) { | ||
for(let h=0;h<multi;h++) { | ||
sum[idxs[i]][h] += data[i][h]; | ||
let idx = idxs[i], vsum = sum[idx], vect = data[i]; | ||
for(let h=0;h<vlen;h++) { | ||
vsum[h] += vect[h]; | ||
} | ||
count[idxs[i]]++; | ||
count[idx]++; | ||
} | ||
// Calculate de average for each centroid | ||
// and de distance between old and new centroids | ||
conv = true; | ||
for(let j=0;j<k;j++) { | ||
for(let h=0;h<multi;h++) { | ||
ks[j][h] = sum[j][h]/count[j] || 0; | ||
dif += old[j][h] - ks[j][h]; | ||
let ksj = ks[j], sumj = sum[j],oldj = old[j], cj = count[j]; | ||
// New average | ||
for(let h=0;h<vlen;h++) { | ||
ksj[h] = sumj[h]/cj || 0; | ||
} | ||
// Find if centroids have moved | ||
if(conv) { | ||
for(let h=0;h<vlen;h++) { | ||
if(oldj[h]!=ksj[h]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
@@ -114,7 +126,14 @@ } | ||
ks[j] = sum[j]/count[j] || 0; | ||
dif += old[j] - ks[j]; | ||
} | ||
// Find if centroids have moved | ||
conv = true; | ||
for(let j=0;j<k;j++) { | ||
if(old[j]!=ks[j]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
conv = (dif===0) || (--it<=0); | ||
conv = conv || (--it<=0); | ||
}while(!conv); | ||
@@ -121,0 +140,0 @@ |
{ | ||
"name": "skmeans", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "Super fast simple kmeans clustering for unidimiensional data", | ||
@@ -5,0 +5,0 @@ "author": "David Gómez Matarrodona <solzimer@gmail.com>", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24327
395