Comparing version 0.9.2 to 0.9.3
@@ -81,6 +81,10 @@ "use strict"; | ||
len = data.length; | ||
var multi = data[0].length > 0; | ||
var map = {}; | ||
// First random centroid | ||
var c = data[Math.floor(Math.random() * len)]; | ||
var key = multi ? c.join("_") : "" + c; | ||
ks.push(c); | ||
map[key] = true; | ||
@@ -132,4 +136,15 @@ // Retrieve next centroids | ||
// this is our new centroid | ||
ks.push(prs[idx - 1].v); | ||
var done = false; | ||
while (!done) { | ||
// this is our new centroid | ||
c = prs[idx - 1].v; | ||
key = multi ? c.join("_") : "" + c; | ||
if (!map[key]) { | ||
map[key] = true; | ||
ks.push(c); | ||
done = true; | ||
} else { | ||
idx++; | ||
} | ||
} | ||
} | ||
@@ -228,3 +243,3 @@ | ||
// Sum values for this centroid | ||
vect = data[_idx2]; // Current vector | ||
vect = data[_i4]; // Current vector | ||
@@ -246,6 +261,7 @@ // Accumulate value on the centroid for current vector | ||
// Old centroid value | ||
cj = count[_j3]; // Number of elements for this centrois | ||
cj = count[_j3]; // Number of elements for this centroid | ||
// New average | ||
for (var _h = 0; _h < vlen; _h++) { | ||
//ksj[h] = (sumj[h]+oldj[h])/(cj+1) || 0; // New centroid | ||
ksj[_h] = sumj[_h] / cj || 0; // New centroid | ||
@@ -252,0 +268,0 @@ } |
@@ -1,3 +0,3 @@ | ||
/*! skmeans 2017-07-19 */ | ||
/*! skmeans 2017-07-20 */ | ||
"use strict";!function r(n,t,o){function a(i,f){if(!t[i]){if(!n[i]){var s="function"==typeof require&&require;if(!f&&s)return s(i,!0);if(e)return e(i,!0);var u=new Error("Cannot find module '"+i+"'");throw u.code="MODULE_NOT_FOUND",u}var v=t[i]={exports:{}};n[i][0].call(v.exports,function(r){var t=n[i][1][r];return a(t||r)},v,v.exports,r,n,t,o)}return t[i].exports}for(var e="function"==typeof require&&require,i=0;i<o.length;i++)a(o[i]);return a}({1:[function(r,n,t){!function(n){var t=r("./main.js");n.skmeans=t}(window)},{"./main.js":4}],2:[function(r,n,t){n.exports={eudist:function(r,n,t){for(var o=r.length,a=0,e=0;e<o;e++){var i=(r[e]||0)-(n[e]||0);a+=i*i}return t?Math.sqrt(a):a},dist:function(r,n,t){var o=Math.abs(r-n);return t?o:o*o}}},{}],3:[function(r,n,t){var o=r("./distance.js"),a=o.eudist,e=o.dist;n.exports={kmrand:function(r,n){for(var t={},o=[],a=n<<2,e=r.length,i=r[0].length>0;o.length<n&&a-- >0;){var f=r[Math.floor(Math.random()*e)],s=i?f.join("_"):""+f;t[s]||(t[s]=!0,o.push(f))}if(o.length<n)throw new Error("Error initializating clusters");return o},kmpp:function(r,n){var t=r[0].length?a:e,o=[],i=r.length,f=r[Math.floor(Math.random()*i)];for(o.push(f);o.length<n;){for(var s=[],u=o.length,v=0,c=[],h=0;h<i;h++){for(var d=1/0,l=0;l<u;l++){var p=t(r[h],o[l]);p<=d&&(d=p)}s[h]=d}for(var g=0;g<i;g++)v+=s[g];for(var m=0;m<i;m++)c[m]={i:m,v:r[m],pr:s[m]/v,cs:0};c.sort(function(r,n){return r.pr-n.pr}),c[0].cs=c[0].pr;for(var k=1;k<i;k++)c[k].cs=c[k-1].cs+c[k].pr;for(var M=Math.random(),j=0;j<i-1&&c[j++].cs>=M;);o.push(c[j-1].v)}return o}}},{"./distance.js":2}],4:[function(r,n,t){function o(r,n,t){t=t||[];for(var o=0;o<r;o++)t[o]=n;return t}var a=r("./distance.js"),e=r("./kinit.js"),i=a.eudist,f=(a.dist,e.kmrand),s=e.kmpp,u=1e4;n.exports=function(r,n,t,a){var e=[],v=[],c=[],h=[],d=!1,l=a||u,p=r.length,g=r[0].length,m=g>0;if(t)e="kmrand"==t?f(r,n):"kmpp"==t?s(r,n):t;else for(var k={};e.length<n;){var M=Math.floor(Math.random()*p);k[M]||(k[M]=!0,e.push(r[M]))}do{for(var j=0;j<p;j++){for(var x=1/0,w=0,q=0;q<n;q++)(h=m?i(r[j],e[q]):Math.abs(r[j]-e[q]))<=x&&(x=h,w=q);c[j]=w}for(var b=[],E=[],v=[],O=0;O<n;O++)b[O]=0,E[O]=m?o(g,0,E[O]):0,v[O]=e[O];if(m){for(var _=0;_<n;_++)e[_]=[];for(var y=0;y<p;y++){for(var D=c[y],N=E[D],U=r[D],z=0;z<g;z++)N[z]+=U[z];b[D]++}d=!0;for(var C=0;C<n;C++){for(var F=e[C],L=E[C],T=v[C],A=b[C],B=0;B<g;B++)F[B]=L[B]/A||0;if(d)for(var G=0;G<g;G++)if(T[G]!=F[G]){d=!1;break}}}else{for(var H=0;H<p;H++){var I=c[H];E[I]+=r[H],b[I]++}for(var J=0;J<n;J++)e[J]=E[J]/b[J]||0;d=!0;for(var K=0;K<n;K++)if(v[K]!=e[K]){d=!1;break}}d=d||--l<=0}while(!d);return{it:u-l,k:n,idxs:c,centroids:e}}},{"./distance.js":2,"./kinit.js":3}]},{},[1]); | ||
"use strict";!function r(n,t,o){function a(i,f){if(!t[i]){if(!n[i]){var s="function"==typeof require&&require;if(!f&&s)return s(i,!0);if(e)return e(i,!0);var u=new Error("Cannot find module '"+i+"'");throw u.code="MODULE_NOT_FOUND",u}var v=t[i]={exports:{}};n[i][0].call(v.exports,function(r){var t=n[i][1][r];return a(t||r)},v,v.exports,r,n,t,o)}return t[i].exports}for(var e="function"==typeof require&&require,i=0;i<o.length;i++)a(o[i]);return a}({1:[function(r,n,t){!function(n){var t=r("./main.js");n.skmeans=t}(window)},{"./main.js":4}],2:[function(r,n,t){n.exports={eudist:function(r,n,t){for(var o=r.length,a=0,e=0;e<o;e++){var i=(r[e]||0)-(n[e]||0);a+=i*i}return t?Math.sqrt(a):a},dist:function(r,n,t){var o=Math.abs(r-n);return t?o:o*o}}},{}],3:[function(r,n,t){var o=r("./distance.js"),a=o.eudist,e=o.dist;n.exports={kmrand:function(r,n){for(var t={},o=[],a=n<<2,e=r.length,i=r[0].length>0;o.length<n&&a-- >0;){var f=r[Math.floor(Math.random()*e)],s=i?f.join("_"):""+f;t[s]||(t[s]=!0,o.push(f))}if(o.length<n)throw new Error("Error initializating clusters");return o},kmpp:function(r,n){var t=r[0].length?a:e,o=[],i=r.length,f=r[0].length>0,s={},u=r[Math.floor(Math.random()*i)],v=f?u.join("_"):""+u;for(o.push(u),s[v]=!0;o.length<n;){for(var c=[],h=o.length,l=0,d=[],p=0;p<i;p++){for(var g=1/0,m=0;m<h;m++){var k=t(r[p],o[m]);k<=g&&(g=k)}c[p]=g}for(var j=0;j<i;j++)l+=c[j];for(var M=0;M<i;M++)d[M]={i:M,v:r[M],pr:c[M]/l,cs:0};d.sort(function(r,n){return r.pr-n.pr}),d[0].cs=d[0].pr;for(var x=1;x<i;x++)d[x].cs=d[x-1].cs+d[x].pr;for(var w=Math.random(),q=0;q<i-1&&d[q++].cs>=w;);for(var _=!1;!_;)u=d[q-1].v,s[v=f?u.join("_"):""+u]?q++:(s[v]=!0,o.push(u),_=!0)}return o}}},{"./distance.js":2}],4:[function(r,n,t){function o(r,n,t){t=t||[];for(var o=0;o<r;o++)t[o]=n;return t}var a=r("./distance.js"),e=r("./kinit.js"),i=a.eudist,f=(a.dist,e.kmrand),s=e.kmpp,u=1e4;n.exports=function(r,n,t,a){var e=[],v=[],c=[],h=[],l=!1,d=a||u,p=r.length,g=r[0].length,m=g>0;if(t)e="kmrand"==t?f(r,n):"kmpp"==t?s(r,n):t;else for(var k={};e.length<n;){var j=Math.floor(Math.random()*p);k[j]||(k[j]=!0,e.push(r[j]))}do{for(var M=0;M<p;M++){for(var x=1/0,w=0,q=0;q<n;q++)(h=m?i(r[M],e[q]):Math.abs(r[M]-e[q]))<=x&&(x=h,w=q);c[M]=w}for(var _=[],b=[],v=[],E=0;E<n;E++)_[E]=0,b[E]=m?o(g,0,b[E]):0,v[E]=e[E];if(m){for(var O=0;O<n;O++)e[O]=[];for(var y=0;y<p;y++){for(var D=c[y],N=b[D],U=r[y],z=0;z<g;z++)N[z]+=U[z];_[D]++}l=!0;for(var C=0;C<n;C++){for(var F=e[C],L=b[C],T=v[C],A=_[C],B=0;B<g;B++)F[B]=L[B]/A||0;if(l)for(var G=0;G<g;G++)if(T[G]!=F[G]){l=!1;break}}}else{for(var H=0;H<p;H++){var I=c[H];b[I]+=r[H],_[I]++}for(var J=0;J<n;J++)e[J]=b[J]/_[J]||0;l=!0;for(var K=0;K<n;K++)if(v[K]!=e[K]){l=!1;break}}l=l||--d<=0}while(!l);return{it:u-d,k:n,idxs:c,centroids:e}}},{"./distance.js":2,"./kinit.js":3}]},{},[1]); |
20
kinit.js
@@ -31,6 +31,10 @@ const | ||
var ks = [], len = data.length; | ||
var multi = data[0].length>0; | ||
var map = {}; | ||
// First random centroid | ||
var c = data[Math.floor(Math.random()*len)]; | ||
var key = multi? c.join("_") : `${c}`; | ||
ks.push(c); | ||
map[key] = true; | ||
@@ -78,4 +82,16 @@ // Retrieve next centroids | ||
// this is our new centroid | ||
ks.push(prs[idx-1].v); | ||
let done = false; | ||
while(!done) { | ||
// this is our new centroid | ||
c = prs[idx-1].v | ||
key = multi? c.join("_") : `${c}`; | ||
if(!map[key]) { | ||
map[key] = true; | ||
ks.push(c); | ||
done = true; | ||
} | ||
else { | ||
idx++; | ||
} | ||
} | ||
} | ||
@@ -82,0 +98,0 @@ |
@@ -79,3 +79,3 @@ /*jshint esversion: 6 */ | ||
vsum = sum[idx], // Sum values for this centroid | ||
vect = data[idx]; // Current vector | ||
vect = data[i]; // Current vector | ||
@@ -94,7 +94,8 @@ // Accumulate value on the centroid for current vector | ||
oldj = old[j], // Old centroid value | ||
cj = count[j]; // Number of elements for this centrois | ||
cj = count[j]; // Number of elements for this centroid | ||
// New average | ||
for(let h=0;h<vlen;h++) { | ||
ksj[h] = sumj[h]/cj || 0; // New centroid | ||
//ksj[h] = (sumj[h]+oldj[h])/(cj+1) || 0; // New centroid | ||
ksj[h] = (sumj[h])/(cj) || 0; // New centroid | ||
} | ||
@@ -101,0 +102,0 @@ // Find if centroids have moved |
{ | ||
"name": "skmeans", | ||
"version": "0.9.2", | ||
"version": "0.9.3", | ||
"description": "Super fast simple kmeans clustering for unidimiensional and multidimensional data. Works in node and browser", | ||
@@ -5,0 +5,0 @@ "author": "David Gómez Matarrodona <solzimer@gmail.com>", |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
66183
23
689
0