Comparing version 0.9.1 to 0.9.2
@@ -0,0 +0,0 @@ "use strict"; |
@@ -56,30 +56,17 @@ "use strict"; | ||
var map = {}, | ||
list = []; | ||
var ks = [], | ||
len = data.length; | ||
ks = [], | ||
t = k << 2; | ||
var len = data.length; | ||
var multi = data[0].length > 0; | ||
for (var i = 0; i < len; i++) { | ||
var d = data[i]; | ||
var key = JSON.stringify(d); | ||
while (ks.length < k && t-- > 0) { | ||
var d = data[Math.floor(Math.random() * len)]; | ||
var key = multi ? d.join("_") : "" + d; | ||
if (!map[key]) { | ||
map[key] = true; | ||
list.push(d); | ||
ks.push(d); | ||
} | ||
}; | ||
if (k > list.length) { | ||
throw new Error("Cluster size greater than distinct data points"); | ||
} else { | ||
var l = list.length, | ||
m = {}; | ||
while (ks.length < k) { | ||
var idx = Math.floor(Math.random() * l); | ||
if (!m[idx]) { | ||
m[idx] = true; | ||
ks.push(list[idx]); | ||
} | ||
} | ||
} | ||
return ks; | ||
if (ks.length < k) throw new Error("Error initializating clusters");else return ks; | ||
}, | ||
@@ -86,0 +73,0 @@ |
@@ -1,3 +0,3 @@ | ||
/*! skmeans 2017-07-18 */ | ||
/*! skmeans 2017-07-19 */ | ||
"use strict";!function r(t,n,a){function o(i,f){if(!n[i]){if(!t[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=n[i]={exports:{}};t[i][0].call(v.exports,function(r){var n=t[i][1][r];return o(n||r)},v,v.exports,r,t,n,a)}return n[i].exports}for(var e="function"==typeof require&&require,i=0;i<a.length;i++)o(a[i]);return o}({1:[function(r,t,n){!function(t){var n=r("./main.js");t.skmeans=n}(window)},{"./main.js":4}],2:[function(r,t,n){t.exports={eudist:function(r,t,n){for(var a=r.length,o=0,e=0;e<a;e++){var i=(r[e]||0)-(t[e]||0);o+=i*i}return n?Math.sqrt(o):o},dist:function(r,t,n){var a=Math.abs(r-t);return n?a:a*a}}},{}],3:[function(r,t,n){var a=r("./distance.js"),o=a.eudist,e=a.dist;t.exports={kmrand:function(r,t){for(var n={},a=[],o=[],e=r.length,i=0;i<e;i++){var f=r[i],s=JSON.stringify(f);n[s]||(n[s]=!0,a.push(f))}if(t>a.length)throw new Error("Cluster size greater than distinct data points");for(var u=a.length,v={};o.length<t;){var h=Math.floor(Math.random()*u);v[h]||(v[h]=!0,o.push(a[h]))}return o},kmpp:function(r,t){var n=r[0].length?o:e,a=[],i=r.length,f=r[Math.floor(Math.random()*i)];for(a.push(f);a.length<t;){for(var s=[],u=a.length,v=0,h=[],c=0;c<i;c++){for(var d=1/0,p=0;p<u;p++){var l=n(r[c],a[p]);l<=d&&(d=l)}s[c]=d}for(var g=0;g<i;g++)v+=s[g];for(var m=0;m<i;m++)h[m]={i:m,v:r[m],pr:s[m]/v,cs:0};h.sort(function(r,t){return r.pr-t.pr}),h[0].cs=h[0].pr;for(var k=1;k<i;k++)h[k].cs=h[k-1].cs+h[k].pr;for(var M=Math.random(),j=0;j<i-1&&h[j++].cs>=M;);a.push(h[j-1].v)}return a}}},{"./distance.js":2}],4:[function(r,t,n){function a(r,t,n){n=n||[];for(var a=0;a<r;a++)n[a]=t;return n}var o=r("./distance.js"),e=r("./kinit.js"),i=o.eudist,f=(o.dist,e.kmrand),s=e.kmpp,u=1e4;t.exports=function(r,t,n,o){var e=[],v=[],h=[],c=[],d=!1,p=o||u,l=r.length,g=r[0].length,m=g>0;if(n)e="kmrand"==n?f(r,t):"kmpp"==n?s(r,t):n;else for(var k={};e.length<t;){var M=Math.floor(Math.random()*l);k[M]||(k[M]=!0,e.push(r[M]))}do{for(var j=0;j<l;j++){for(var x=1/0,w=0,q=0;q<t;q++)(c=m?i(r[j],e[q]):Math.abs(r[j]-e[q]))<=x&&(x=c,w=q);h[j]=w}for(var b=[],O=[],v=[],y=0;y<t;y++)b[y]=0,O[y]=m?a(g,0,O[y]):0,v[y]=e[y];if(m){for(var E=0;E<t;E++)e[E]=[];for(var N=0;N<l;N++){for(var C=h[N],D=O[C],U=r[C],_=0;_<g;_++)D[_]+=U[_];b[C]++}d=!0;for(var z=0;z<t;z++){for(var F=e[z],J=O[z],L=v[z],S=b[z],T=0;T<g;T++)F[T]=J[T]/S||0;if(d)for(var A=0;A<g;A++)if(L[A]!=F[A]){d=!1;break}}}else{for(var B=0;B<l;B++){var G=h[B];O[G]+=r[B],b[G]++}for(var H=0;H<t;H++)e[H]=O[H]/b[H]||0;d=!0;for(var I=0;I<t;I++)if(v[I]!=e[I]){d=!1;break}}d=d||--p<=0}while(!d);return{it:u-p,k:t,idxs:h,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[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]); |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = function(grunt) { |
30
kinit.js
@@ -8,29 +8,17 @@ const | ||
kmrand(data,k) { | ||
var map = {}, list = []; | ||
var ks = [], len = data.length | ||
var map = {}, ks = [], t = k<<2; | ||
var len = data.length; | ||
var multi = data[0].length>0; | ||
for(let i=0;i<len;i++) { | ||
let d = data[i]; | ||
var key = JSON.stringify(d); | ||
while(ks.length<k && (t--)>0) { | ||
let d = data[Math.floor(Math.random()*len)]; | ||
let key = multi? d.join("_") : `${d}`; | ||
if(!map[key]) { | ||
map[key] = true; | ||
list.push(d); | ||
ks.push(d); | ||
} | ||
}; | ||
if(k>list.length) { | ||
throw new Error("Cluster size greater than distinct data points"); | ||
} | ||
else { | ||
let l = list.length, m = {}; | ||
while(ks.length<k) { | ||
let idx = Math.floor(Math.random()*l); | ||
if(!m[idx]) { | ||
m[idx] = true; | ||
ks.push(list[idx]); | ||
} | ||
} | ||
} | ||
return ks; | ||
if(ks.length<k) throw new Error("Error initializating clusters"); | ||
else return ks; | ||
}, | ||
@@ -37,0 +25,0 @@ |
@@ -0,0 +0,0 @@ /*jshint esversion: 6 */ |
{ | ||
"name": "skmeans", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"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>", |
@@ -0,0 +0,0 @@ # skmeans |
@@ -0,0 +0,0 @@ const skmeans = require("../main.js"); |
@@ -0,0 +0,0 @@ const skmeans = require("../main.js"); |
@@ -0,0 +0,0 @@ const skmeans = require("../main.js"); |
@@ -0,0 +0,0 @@ const skmeans = require("../main.js"); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
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
58537
18
631