Comparing version 0.8.0 to 0.8.1
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
/*! skmeans 2017-07-17 */ | ||
"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 c=t[f]={exports:{}};n[f][0].call(c.exports,function(r){var t=n[f][1][r];return a(t||r)},c,c.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){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 f(r,n,t){t=t||[];for(var o=0;o<r;o++)t[o]=n;return t}var i=1e4;n.exports=function(r,n,t,a){var u=[],v=[],c=[],s=[],h=!1,p=a||i,l=r.length,m=r[0].length,d=m>0;if(t)u="kmpp"==t?e(r,n):t;else for(var M=0;M<n;M++)u.push(r[Math.floor(Math.random()*l)]);do{for(var g=0;g<l;g++){for(var x=1/0,k=0,q=0;q<n;q++)(s=d?o(r[g],u[q]):Math.abs(r[g]-u[q]))<x&&(x=s,k=q);c[g]=k}for(var w=[],b=[],v=[],y=0;y<n;y++)w[y]=0,b[y]=d?f(m,0,b[y]):0,v[y]=u[y];if(d){for(var E=0;E<n;E++)u[E]=[];for(var O=0;O<l;O++){for(var j=c[O],D=b[j],N=r[O],U=0;U<m;U++)D[U]+=N[U];w[j]++}h=!0;for(var _=0;_<n;_++){for(var C=u[_],F=b[_],L=v[_],T=w[_],z=0;z<m;z++)C[z]=F[z]/T||0;if(h)for(var A=0;A<m;A++)if(L[A]!=C[A]){h=!1;break}}}else{for(var B=0;B<l;B++){var G=c[B];b[G]+=r[B],w[G]++}for(var H=0;H<n;H++)u[H]=b[H]/w[H]||0;h=!0;for(var I=0;I<n;I++)if(v[I]!=u[I]){h=!1;break}}h=h||--p<=0}while(!h);return{it:i-p,k:n,idxs:c,centroids:u}}},{}]},{},[1]); |
@@ -0,0 +0,0 @@ module.exports = function(grunt) { |
42
main.js
@@ -28,2 +28,33 @@ /*jshint esversion: 6 */ | ||
function kmrand(data,k) { | ||
var map = {}, list = []; | ||
var ks = []; | ||
data.forEach(d=>{ | ||
var key = JSON.stringify(d); | ||
map[key] = map[k]||d; | ||
}); | ||
for(var key in map) list.push(map[key]); | ||
if(k>list.length) { | ||
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; | ||
} | ||
} | ||
} | ||
} | ||
return ks; | ||
} | ||
/** | ||
@@ -85,6 +116,8 @@ * K-means++ initial centroid selection | ||
if(!initial) { | ||
for(let i=0;i<k;i++) { | ||
for(let i=0;i<k;i++) | ||
ks.push(data[Math.floor(Math.random()*len)]); | ||
} | ||
} | ||
else if(initial=="kmrand") { | ||
ks = kmrand(data,k); | ||
} | ||
else if(initial=="kmpp") { | ||
@@ -104,3 +137,3 @@ ks = kmpp(data,k); | ||
var dist = multi? eudist(data[i],ks[j]) : Math.abs(data[i]-ks[j]); | ||
if(dist<min) { | ||
if(dist<=min) { | ||
min = dist; | ||
@@ -130,3 +163,3 @@ idx = j; | ||
vsum = sum[idx], // Sum values for this centroid | ||
vect = data[i]; // Current vector | ||
vect = data[idx]; // Current vector | ||
@@ -150,2 +183,3 @@ // Accumulate value on the centroid for current vector | ||
ksj[h] = sumj[h]/cj || 0; // New centroid | ||
if(ksj[h]==0) debugger; | ||
} | ||
@@ -152,0 +186,0 @@ // Find if centroids have moved |
{ | ||
"name": "skmeans", | ||
"version": "0.8.0", | ||
"version": "0.8.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>", |
@@ -53,3 +53,5 @@ # skmeans | ||
* **k** Number of clusters | ||
* **centroids** Optional. Initial centroid values. If not provided, the algorith will try to choose an apropiate ones. You can pass **"kmpp"** as argument, so the algorythm will use the [k-means++](https://en.wikipedia.org/wiki/K-means%2B%2B) cluster initialization method. | ||
* **centroids** Optional. Initial centroid values. If not provided, the algorith will try to choose an apropiate ones. Alternative values can be: | ||
* **"kmrand"** Cluster initialization will be random, but with extra checking, so there will no be two equal initial centroids. | ||
* **"kmpp"** The algorythm will use the [k-means++](https://en.wikipedia.org/wiki/K-means%2B%2B) cluster initialization method. | ||
* **iterations** Optional. Maximum number of iterations. If not provided, it will be set to 10000. | ||
@@ -56,0 +58,0 @@ |
@@ -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
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
165206
17
3731
63
1