Comparing version
@@ -0,0 +0,0 @@ "use strict"; |
@@ -185,4 +185,9 @@ "use strict"; | ||
if (!initial) { | ||
for (var i = 0; i < k; i++) { | ||
ks.push(data[Math.floor(Math.random() * len)]); | ||
var _idxs = {}; | ||
while (ks.length < k) { | ||
var idx = Math.floor(Math.random() * len); | ||
if (!_idxs[idx]) { | ||
_idxs[idx] = true; | ||
ks.push(data[idx]); | ||
} | ||
} | ||
@@ -199,14 +204,14 @@ } else if (initial == "kmrand") { | ||
// For each value in data, find the nearest centroid | ||
for (var _i4 = 0; _i4 < len; _i4++) { | ||
for (var i = 0; i < len; i++) { | ||
var min = Infinity, | ||
idx = 0; | ||
_idx = 0; | ||
for (var j = 0; j < k; j++) { | ||
// Multidimensional or unidimensional | ||
var dist = multi ? eudist(data[_i4], ks[j]) : Math.abs(data[_i4] - ks[j]); | ||
var dist = multi ? eudist(data[i], ks[j]) : Math.abs(data[i] - ks[j]); | ||
if (dist <= min) { | ||
min = dist; | ||
idx = j; | ||
_idx = j; | ||
} | ||
} | ||
idxs[_i4] = idx; | ||
idxs[i] = _idx; | ||
} | ||
@@ -231,8 +236,8 @@ | ||
} // Sum values and count for each centroid | ||
for (var _i5 = 0; _i5 < len; _i5++) { | ||
var _idx = idxs[_i5], | ||
for (var _i4 = 0; _i4 < len; _i4++) { | ||
var _idx2 = idxs[_i4], | ||
// Centroid for that item | ||
vsum = sum[_idx], | ||
vsum = sum[_idx2], | ||
// Sum values for this centroid | ||
vect = data[_idx]; // Current vector | ||
vect = data[_idx2]; // Current vector | ||
@@ -243,3 +248,3 @@ // Accumulate value on the centroid for current vector | ||
} | ||
count[_idx]++; // Number of values for this centroid | ||
count[_idx2]++; // Number of values for this centroid | ||
} | ||
@@ -275,6 +280,6 @@ // Calculate the average for each centroid | ||
// Sum values and count for each centroid | ||
for (var _i6 = 0; _i6 < len; _i6++) { | ||
var _idx2 = idxs[_i6]; | ||
sum[_idx2] += data[_i6]; | ||
count[_idx2]++; | ||
for (var _i5 = 0; _i5 < len; _i5++) { | ||
var _idx3 = idxs[_i5]; | ||
sum[_idx3] += data[_i5]; | ||
count[_idx3]++; | ||
} | ||
@@ -281,0 +286,0 @@ // Calculate the average for each centroid |
/*! skmeans 2017-07-18 */ | ||
"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 c=Math.floor(Math.random()*u);v[c]||(v[c]=!0,o.push(a[c]))}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,c=[],h=0;h<i;h++){for(var d=1/0,p=0;p<u;p++){var l=n(r[h],a[p]);l<=d&&(d=l)}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,t){return r.pr-t.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;);a.push(c[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=[],c=[],h=[],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=0;k<t;k++)e.push(r[Math.floor(Math.random()*l)]);do{for(var M=0;M<l;M++){for(var j=1/0,x=0,w=0;w<t;w++)(h=m?i(r[M],e[w]):Math.abs(r[M]-e[w]))<=j&&(j=h,x=w);c[M]=x}for(var q=[],b=[],v=[],O=0;O<t;O++)q[O]=0,b[O]=m?a(g,0,b[O]):0,v[O]=e[O];if(m){for(var y=0;y<t;y++)e[y]=[];for(var E=0;E<l;E++){for(var N=c[E],C=b[N],D=r[N],U=0;U<g;U++)C[U]+=D[U];q[N]++}d=!0;for(var _=0;_<t;_++){for(var z=e[_],F=b[_],J=v[_],L=q[_],S=0;S<g;S++)z[S]=F[S]/L||0;if(d)for(var T=0;T<g;T++)if(J[T]!=z[T]){d=!1;break}}}else{for(var A=0;A<l;A++){var B=c[A];b[B]+=r[A],q[B]++}for(var G=0;G<t;G++)e[G]=b[G]/q[G]||0;d=!0;for(var H=0;H<t;H++)if(v[H]!=e[H]){d=!1;break}}d=d||--p<=0}while(!d);return{it:u-p,k:t,idxs:c,centroids:e}}},{"./distance.js":2,"./kinit.js":3}]},{},[1]); | ||
"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]); |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ module.exports = function(grunt) { |
@@ -0,0 +0,0 @@ const |
10
main.js
@@ -28,4 +28,10 @@ /*jshint esversion: 6 */ | ||
if(!initial) { | ||
for(let i=0;i<k;i++) | ||
ks.push(data[Math.floor(Math.random()*len)]); | ||
let idxs = {}; | ||
while(ks.length<k) { | ||
let idx = Math.floor(Math.random()*len); | ||
if(!idxs[idx]) { | ||
idxs[idx] = true; | ||
ks.push(data[idx]); | ||
} | ||
} | ||
} | ||
@@ -32,0 +38,0 @@ else if(initial=="kmrand") { |
{ | ||
"name": "skmeans", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"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"); |
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
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
173090
199.06%19
5.56%3859
542.1%1
Infinity%