Comparing version
@@ -0,0 +0,0 @@ "use strict"; |
@@ -53,25 +53,24 @@ "use strict"; | ||
list = []; | ||
var ks = []; | ||
var ks = [], | ||
len = data.length; | ||
data.forEach(function (d) { | ||
for (var i = 0; i < len; i++) { | ||
var d = data[i]; | ||
var key = JSON.stringify(d); | ||
map[key] = map[k] || d; | ||
}); | ||
for (var key in map) { | ||
list.push(map[key]); | ||
}if (k > list.length) { | ||
if (!map[key]) { | ||
map[key] = true; | ||
list.push(d); | ||
} | ||
}; | ||
if (k > list.length) { | ||
throw new Error("Cluster size greater than distinct data points"); | ||
} else { | ||
var len = data.length, | ||
map = {}; | ||
for (var i = 0; i < k; i++) { | ||
var b = false; | ||
while (!b) { | ||
var v = list[Math.floor(Math.random() * len)]; | ||
var key = JSON.stringify(v); | ||
if (!map[key]) { | ||
ks.push(v); | ||
map[key] = true; | ||
b = true; | ||
} | ||
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]); | ||
} | ||
@@ -81,2 +80,3 @@ } | ||
console.log(ks); | ||
return ks; | ||
@@ -83,0 +83,0 @@ } |
/*! skmeans 2017-07-17 */ | ||
"use strict";!function r(n,t,o){function a(e,i){if(!t[e]){if(!n[e]){var u="function"==typeof require&&require;if(!i&&u)return u(e,!0);if(f)return f(e,!0);var v=new Error("Cannot find module '"+e+"'");throw v.code="MODULE_NOT_FOUND",v}var c=t[e]={exports:{}};n[e][0].call(c.exports,function(r){var t=n[e][1][r];return a(t||r)},c,c.exports,r,n,t,o)}return t[e].exports}for(var f="function"==typeof require&&require,e=0;e<o.length;e++)a(o[e]);return a}({1:[function(r,n,t){!function(n){var t=r("./main.js");n.skmeans=t}(window)},{"./main.js":2}],2:[function(r,n,t){function o(r,n,t){for(var o=r.length,a=0,f=0;f<o;f++){var e=(r[f]||0)-(n[f]||0);a+=e*e}return t?Math.sqrt(a):a}function a(r,n,t){var o=Math.abs(r-n);return t?o:o*o}function f(r,n){var t={},o=[],a=[];r.forEach(function(r){var o=JSON.stringify(r);t[o]=t[n]||r});for(var f in t)o.push(t[f]);if(n>o.length)throw new Error("Cluster size greater than distinct data points");for(var e=r.length,t={},i=0;i<n;i++)for(var u=!1;!u;){var v=o[Math.floor(Math.random()*e)],f=JSON.stringify(v);t[f]||(a.push(v),t[f]=!0,u=!0)}return a}function e(r,n){var t=this,f=r[0].length?o:a,e=[],i=r.length,u=r[Math.floor(Math.random()*i)];e.push(u);for(;e.length<n;)!function(){var n=r.map(function(r){var n=e.map(function(n){return f(r,n)});return Math.min.apply(t,n)}),o=n.reduce(function(r,n){return r+n},0),a=n.map(function(n,t){return{i:t,v:r[t],pr:n/o}});a.sort(function(r,n){return r.pr-n.pr}),a.forEach(function(r,n){r.cs=r.pr+(n>0?a[n-1].cs:0)});var i=Math.random(),u=a.filter(function(r){return r.cs>=i});e.push(u[0].v)}();return e}function i(r,n,t){t=t||[];for(var o=0;o<r;o++)t[o]=n;return t}var u=1e4;n.exports=function(r,n,t,a){var v=[],c=[],s=[],h=[],p=!1,l=a||u,d=r.length,m=r[0].length,g=m>0;if(t)v="kmrand"==t?f(r,n):"kmpp"==t?e(r,n):t;else for(var M=0;M<n;M++)v.push(r[Math.floor(Math.random()*d)]);do{for(var w=0;w<d;w++){for(var k=1/0,x=0,q=0;q<n;q++)(h=g?o(r[w],v[q]):Math.abs(r[w]-v[q]))<=k&&(k=h,x=q);s[w]=x}for(var y=[],E=[],c=[],O=0;O<n;O++)y[O]=0,E[O]=g?i(m,0,E[O]):0,c[O]=v[O];if(g){for(var b=0;b<n;b++)v[b]=[];for(var N=0;N<d;N++){for(var j=s[N],C=E[j],D=r[j],J=0;J<m;J++)C[J]+=D[J];y[j]++}p=!0;for(var S=0;S<n;S++){for(var U=v[S],_=E[S],z=c[S],F=y[S],L=0;L<m;L++)U[L]=_[L]/F||0;if(p)for(var T=0;T<m;T++)if(z[T]!=U[T]){p=!1;break}}}else{for(var A=0;A<d;A++){var B=s[A];E[B]+=r[A],y[B]++}for(var G=0;G<n;G++)v[G]=E[G]/y[G]||0;p=!0;for(var H=0;H<n;H++)if(c[H]!=v[H]){p=!1;break}}p=p||--l<=0}while(!p);return{it:u-l,k:n,idxs:s,centroids:v}}},{}]},{},[1]); | ||
"use strict";!function r(n,t,o){function a(f,i){if(!t[f]){if(!n[f]){var u="function"==typeof require&&require;if(!i&&u)return u(f,!0);if(e)return e(f,!0);var v=new Error("Cannot find module '"+f+"'");throw v.code="MODULE_NOT_FOUND",v}var s=t[f]={exports:{}};n[f][0].call(s.exports,function(r){var t=n[f][1][r];return a(t||r)},s,s.exports,r,n,t,o)}return t[f].exports}for(var e="function"==typeof require&&require,f=0;f<o.length;f++)a(o[f]);return a}({1:[function(r,n,t){!function(n){var t=r("./main.js");n.skmeans=t}(window)},{"./main.js":2}],2:[function(r,n,t){function o(r,n,t){for(var o=r.length,a=0,e=0;e<o;e++){var f=(r[e]||0)-(n[e]||0);a+=f*f}return t?Math.sqrt(a):a}function a(r,n,t){var o=Math.abs(r-n);return t?o:o*o}function e(r,n){for(var t={},o=[],a=[],e=r.length,f=0;f<e;f++){var i=r[f],u=JSON.stringify(i);t[u]||(t[u]=!0,o.push(i))}if(n>o.length)throw new Error("Cluster size greater than distinct data points");for(var v=o.length,s={};a.length<n;){var c=Math.floor(Math.random()*v);s[c]||(s[c]=!0,a.push(o[c]))}return console.log(a),a}function f(r,n){var t=this,e=r[0].length?o:a,f=[],i=r.length,u=r[Math.floor(Math.random()*i)];f.push(u);for(;f.length<n;)!function(){var n=r.map(function(r){var n=f.map(function(n){return e(r,n)});return Math.min.apply(t,n)}),o=n.reduce(function(r,n){return r+n},0),a=n.map(function(n,t){return{i:t,v:r[t],pr:n/o}});a.sort(function(r,n){return r.pr-n.pr}),a.forEach(function(r,n){r.cs=r.pr+(n>0?a[n-1].cs:0)});var i=Math.random(),u=a.filter(function(r){return r.cs>=i});f.push(u[0].v)}();return f}function i(r,n,t){t=t||[];for(var o=0;o<r;o++)t[o]=n;return t}var u=1e4;n.exports=function(r,n,t,a){var v=[],s=[],c=[],h=[],l=!1,p=a||u,d=r.length,g=r[0].length,m=g>0;if(t)v="kmrand"==t?e(r,n):"kmpp"==t?f(r,n):t;else for(var M=0;M<n;M++)v.push(r[Math.floor(Math.random()*d)]);do{for(var w=0;w<d;w++){for(var k=1/0,x=0,q=0;q<n;q++)(h=m?o(r[w],v[q]):Math.abs(r[w]-v[q]))<=k&&(k=h,x=q);c[w]=x}for(var b=[],y=[],s=[],E=0;E<n;E++)b[E]=0,y[E]=m?i(g,0,y[E]):0,s[E]=v[E];if(m){for(var O=0;O<n;O++)v[O]=[];for(var N=0;N<d;N++){for(var j=c[N],C=y[j],D=r[j],U=0;U<g;U++)C[U]+=D[U];b[j]++}l=!0;for(var _=0;_<n;_++){for(var z=v[_],F=y[_],J=s[_],L=b[_],S=0;S<g;S++)z[S]=F[S]/L||0;if(l)for(var T=0;T<g;T++)if(J[T]!=z[T]){l=!1;break}}}else{for(var A=0;A<d;A++){var B=c[A];y[B]+=r[A],b[B]++}for(var G=0;G<n;G++)v[G]=y[G]/b[G]||0;l=!0;for(var H=0;H<n;H++)if(s[H]!=v[H]){l=!1;break}}l=l||--p<=0}while(!l);return{it:u-p,k:n,idxs:c,centroids:v}}},{}]},{},[1]); |
@@ -0,0 +0,0 @@ module.exports = function(grunt) { |
32
main.js
@@ -30,9 +30,13 @@ /*jshint esversion: 6 */ | ||
var map = {}, list = []; | ||
var ks = []; | ||
var ks = [], len = data.length | ||
data.forEach(d=>{ | ||
for(let i=0;i<len;i++) { | ||
let d = data[i]; | ||
var key = JSON.stringify(d); | ||
map[key] = map[k]||d; | ||
}); | ||
for(var key in map) list.push(map[key]); | ||
if(!map[key]) { | ||
map[key] = true; | ||
list.push(d); | ||
} | ||
}; | ||
if(k>list.length) { | ||
@@ -42,13 +46,8 @@ throw new Error("Cluster size greater than distinct data points"); | ||
else { | ||
var len = data.length, map = {}; | ||
for(let i=0;i<k;i++) { | ||
var b = false; | ||
while(!b) { | ||
var v = list[Math.floor(Math.random()*len)]; | ||
var key = JSON.stringify(v); | ||
if(!map[key]) { | ||
ks.push(v); | ||
map[key] = true; | ||
b = true; | ||
} | ||
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]); | ||
} | ||
@@ -58,2 +57,3 @@ } | ||
console.log(ks); | ||
return ks; | ||
@@ -60,0 +60,0 @@ } |
{ | ||
"name": "skmeans", | ||
"version": "0.8.3", | ||
"version": "0.8.4", | ||
"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"); |
@@ -5,4 +5,4 @@ const skmeans = require("../main.js"); | ||
for(var i=0;i<1000;i++) { | ||
var res = skmeans(data,16); | ||
console.log(res.it,res.centroids,"kmpp"); | ||
var res = skmeans(data,16,"kmrand"); | ||
console.log(res.it,res.centroids); | ||
} |
@@ -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
54390
-67.67%16
-5.88%555
-85.25%1
Infinity%