Comparing version 0.6.1 to 0.7.0
@@ -38,3 +38,4 @@ "use strict"; | ||
} | ||
return Math.sqrt(sum); | ||
// Square root not really needed | ||
return sum; //Math.sqrt(sum); | ||
} | ||
@@ -55,2 +56,9 @@ | ||
function equals(v1, v2, multi) { | ||
var l = v1.length; | ||
for (var i = 0; i < l; i++) { | ||
if (v1[i] != v2[i]) return false; | ||
}return true; | ||
} | ||
/** | ||
@@ -68,7 +76,9 @@ * Inits an array with values | ||
var ks = [], | ||
old = [], | ||
idxs = [], | ||
len = data.length; | ||
dist = []; | ||
var conv = false, | ||
it = maxit || MAX; | ||
var vlen = data[0].length, | ||
var len = data.length, | ||
vlen = data[0].length, | ||
multi = vlen > 0; | ||
@@ -119,20 +129,27 @@ | ||
var _idx = idxs[_i2], | ||
vsum = sum[_idx], | ||
vect = data[_i2]; | ||
// Centroid for that item | ||
vsum = sum[_idx], | ||
// Sum values for this centroid | ||
vect = data[_i2]; // Current vector | ||
// Accumulate value on the centroid for current vector | ||
for (var h = 0; h < vlen; h++) { | ||
vsum[h] += vect[h]; | ||
} | ||
count[_idx]++; | ||
count[_idx]++; // Number of values for this centroid | ||
} | ||
// Calculate de average for each centroid | ||
// and de distance between old and new centroids | ||
// Calculate the average for each centroid | ||
conv = true; | ||
for (var _j3 = 0; _j3 < k; _j3++) { | ||
var ksj = ks[_j3], | ||
sumj = sum[_j3], | ||
oldj = old[_j3], | ||
cj = count[_j3]; | ||
// Current centroid | ||
sumj = sum[_j3], | ||
// Accumulated centroid values | ||
oldj = old[_j3], | ||
// Old centroid value | ||
cj = count[_j3]; // Number of elements for this centrois | ||
// New average | ||
for (var _h = 0; _h < vlen; _h++) { | ||
ksj[_h] = sumj[_h] / cj || 0; | ||
ksj[_h] = sumj[_h] / cj || 0; // New centroid | ||
} | ||
@@ -154,9 +171,9 @@ // Find if centroids have moved | ||
for (var _i3 = 0; _i3 < len; _i3++) { | ||
sum[idxs[_i3]] += data[_i3]; | ||
count[idxs[_i3]]++; | ||
var _idx2 = idxs[_i3]; | ||
sum[_idx2] += data[_i3]; | ||
count[_idx2]++; | ||
} | ||
// Calculate de average for each centroid | ||
// and de distance between old and new centroids | ||
// Calculate the average for each centroid | ||
for (var _j4 = 0; _j4 < k; _j4++) { | ||
ks[_j4] = sum[_j4] / count[_j4] || 0; | ||
ks[_j4] = sum[_j4] / count[_j4] || 0; // New centroid | ||
} | ||
@@ -163,0 +180,0 @@ // Find if centroids have moved |
@@ -1,3 +0,3 @@ | ||
/*! skmeans 2017-06-18 */ | ||
/*! skmeans 2017-07-14 */ | ||
"use strict";!function r(o,n,e){function t(a,i){if(!n[a]){if(!o[a]){var u="function"==typeof require&&require;if(!i&&u)return u(a,!0);if(f)return f(a,!0);var v=new Error("Cannot find module '"+a+"'");throw v.code="MODULE_NOT_FOUND",v}var s=n[a]={exports:{}};o[a][0].call(s.exports,function(r){var n=o[a][1][r];return t(n||r)},s,s.exports,r,o,n,e)}return n[a].exports}for(var f="function"==typeof require&&require,a=0;a<e.length;a++)t(e[a]);return t}({1:[function(r,o,n){!function(o){var n=r("./main.js");o.skmeans=n}(window)},{"./main.js":2}],2:[function(r,o,n){function e(r,o){for(var n=r.length,e=0,t=0;t<n;t++){var f=(r[t]||0)-(o[t]||0);e+=f*f}return Math.sqrt(e)}function t(r,o,n){n=n||[];for(var e=0;e<r;e++)n[e]=o;return n}var f=1e4;o.exports=function(r,o,n,a){var i=[],u=[],v=r.length,s=!1,c=a||f,h=r[0].length,l=h>0;if(n)i=n;else for(var d=0;d<o;d++)i.push(r[Math.floor(Math.random()*v)]);do{for(var p=0;p<v;p++){for(var x=1/0,m=0,q=0;q<o;q++){var w=l?e(r[p],i[q]):Math.abs(r[p]-i[q]);w<x&&(x=w,m=q)}u[p]=m}for(var M=[],g=[],k=[],b=0;b<o;b++)M[b]=0,g[b]=l?t(h,0,g[b]):0,k[b]=i[b];if(l){for(var O=0;O<o;O++)i[O]=[];for(var j=0;j<v;j++){for(var y=u[j],D=g[y],E=r[j],N=0;N<h;N++)D[N]+=E[N];M[y]++}s=!0;for(var U=0;U<o;U++){for(var _=i[U],C=g[U],F=k[U],L=M[U],T=0;T<h;T++)_[T]=C[T]/L||0;if(s)for(var z=0;z<h;z++)if(F[z]!=_[z]){s=!1;break}}}else{for(var A=0;A<v;A++)g[u[A]]+=r[A],M[u[A]]++;for(var B=0;B<o;B++)i[B]=g[B]/M[B]||0;s=!0;for(var G=0;G<o;G++)if(k[G]!=i[G]){s=!1;break}}s=s||--c<=0}while(!s);return{it:f-c,k:o,idxs:u,centroids:i}}},{}]},{},[1]); | ||
"use strict";!function r(o,n,e){function f(a,i){if(!n[a]){if(!o[a]){var u="function"==typeof require&&require;if(!i&&u)return u(a,!0);if(t)return t(a,!0);var v=new Error("Cannot find module '"+a+"'");throw v.code="MODULE_NOT_FOUND",v}var s=n[a]={exports:{}};o[a][0].call(s.exports,function(r){var n=o[a][1][r];return f(n||r)},s,s.exports,r,o,n,e)}return n[a].exports}for(var t="function"==typeof require&&require,a=0;a<e.length;a++)f(e[a]);return f}({1:[function(r,o,n){!function(o){var n=r("./main.js");o.skmeans=n}(window)},{"./main.js":2}],2:[function(r,o,n){function e(r,o){for(var n=r.length,e=0,f=0;f<n;f++){var t=(r[f]||0)-(o[f]||0);e+=t*t}return e}function f(r,o,n){n=n||[];for(var e=0;e<r;e++)n[e]=o;return n}var t=1e4;o.exports=function(r,o,n,a){var i=[],u=[],v=[],s=[],c=!1,l=a||t,h=r.length,d=r[0].length,p=d>0;if(n)i=n;else for(var x=0;x<o;x++)i.push(r[Math.floor(Math.random()*h)]);do{for(var m=0;m<h;m++){for(var w=1/0,g=0,k=0;k<o;k++)(s=p?e(r[m],i[k]):Math.abs(r[m]-i[k]))<w&&(w=s,g=k);v[m]=g}for(var q=[],M=[],u=[],b=0;b<o;b++)q[b]=0,M[b]=p?f(d,0,M[b]):0,u[b]=i[b];if(p){for(var O=0;O<o;O++)i[O]=[];for(var j=0;j<h;j++){for(var y=v[j],D=M[y],E=r[j],N=0;N<d;N++)D[N]+=E[N];q[y]++}c=!0;for(var U=0;U<o;U++){for(var _=i[U],C=M[U],F=u[U],L=q[U],T=0;T<d;T++)_[T]=C[T]/L||0;if(c)for(var z=0;z<d;z++)if(F[z]!=_[z]){c=!1;break}}}else{for(var A=0;A<h;A++){var B=v[A];M[B]+=r[A],q[B]++}for(var G=0;G<o;G++)i[G]=M[G]/q[G]||0;c=!0;for(var H=0;H<o;H++)if(u[H]!=i[H]){c=!1;break}}c=c||--l<=0}while(!c);return{it:t-l,k:o,idxs:v,centroids:i}}},{}]},{},[1]); |
67
main.js
@@ -9,10 +9,11 @@ /*jshint esversion: 6 */ | ||
function eudist(v1,v2) { | ||
var len = v1.length; | ||
var sum = 0; | ||
var len = v1.length; | ||
var sum = 0; | ||
for(let i=0;i<len;i++) { | ||
var d = (v1[i]||0) - (v2[i]||0); | ||
sum += d*d; | ||
} | ||
return Math.sqrt(sum); | ||
for(let i=0;i<len;i++) { | ||
var d = (v1[i]||0) - (v2[i]||0); | ||
sum += d*d; | ||
} | ||
// Square root not really needed | ||
return sum; //Math.sqrt(sum); | ||
} | ||
@@ -24,11 +25,18 @@ | ||
function mandist(v1,v2) { | ||
var len = v1.length; | ||
var sum = 0; | ||
var len = v1.length; | ||
var sum = 0; | ||
for(let i=0;i<len;i++) { | ||
sum += Math.abs((v1[i]||0) - (v2[i]||0)); | ||
} | ||
return sum; | ||
for(let i=0;i<len;i++) { | ||
sum += Math.abs((v1[i]||0) - (v2[i]||0)); | ||
} | ||
return sum; | ||
} | ||
function equals(v1,v2,multi) { | ||
var l = v1.length; | ||
for(var i=0;i<l;i++) | ||
if(v1[i]!=v2[i]) return false; | ||
return true; | ||
} | ||
/** | ||
@@ -44,5 +52,5 @@ * Inits an array with values | ||
function skmeans(data,k,initial,maxit) { | ||
var ks = [], idxs = [], len = data.length; | ||
var ks = [], old = [], idxs = [], dist = []; | ||
var conv = false, it = maxit || MAX; | ||
var vlen = data[0].length, multi = vlen>0; | ||
var len = data.length, vlen = data[0].length, multi = vlen>0; | ||
@@ -88,16 +96,23 @@ if(!initial) { | ||
for(let i=0;i<len;i++) { | ||
let idx = idxs[i], vsum = sum[idx], vect = data[i]; | ||
let idx = idxs[i], // Centroid for that item | ||
vsum = sum[idx], // Sum values for this centroid | ||
vect = data[i]; // Current vector | ||
// Accumulate value on the centroid for current vector | ||
for(let h=0;h<vlen;h++) { | ||
vsum[h] += vect[h]; | ||
} | ||
count[idx]++; | ||
count[idx]++; // Number of values for this centroid | ||
} | ||
// Calculate de average for each centroid | ||
// and de distance between old and new centroids | ||
// Calculate the average for each centroid | ||
conv = true; | ||
for(let j=0;j<k;j++) { | ||
let ksj = ks[j], sumj = sum[j],oldj = old[j], cj = count[j]; | ||
let ksj = ks[j], // Current centroid | ||
sumj = sum[j], // Accumulated centroid values | ||
oldj = old[j], // Old centroid value | ||
cj = count[j]; // Number of elements for this centrois | ||
// New average | ||
for(let h=0;h<vlen;h++) { | ||
ksj[h] = sumj[h]/cj || 0; | ||
ksj[h] = sumj[h]/cj || 0; // New centroid | ||
} | ||
@@ -119,9 +134,9 @@ // Find if centroids have moved | ||
for(let i=0;i<len;i++) { | ||
sum[idxs[i]] += data[i]; | ||
count[idxs[i]]++; | ||
let idx = idxs[i]; | ||
sum[idx] += data[i]; | ||
count[idx]++; | ||
} | ||
// Calculate de average for each centroid | ||
// and de distance between old and new centroids | ||
// Calculate the average for each centroid | ||
for(let j=0;j<k;j++) { | ||
ks[j] = sum[j]/count[j] || 0; | ||
ks[j] = sum[j]/count[j] || 0; // New centroid | ||
} | ||
@@ -128,0 +143,0 @@ // Find if centroids have moved |
{ | ||
"name": "skmeans", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"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>", |
@@ -49,3 +49,3 @@ # skmeans | ||
### skmeans(data,k,[centroids],[iterations]) | ||
Calculates unidimiensional k-means clustering on *data*. Parameters are: | ||
Calculates unidimiensional and multidimensional k-means clustering on *data*. Parameters are: | ||
* **data** Unidimiensional or multidimensional array of values to be clustered. for unidimiensional data, takes the form of a simple array *[1,2,3.....,n]*. For multidimensional data, takes a | ||
@@ -52,0 +52,0 @@ NxM array *[[1,2],[2,3]....[n,m]]* |
const skmeans = require("../main.js"); | ||
var data = []; | ||
for(let i=0;i<20;i++) { | ||
for(let i=0;i<2000;i++) { | ||
var r = Math.random(); | ||
@@ -12,3 +12,2 @@ if(r>=0 && r<0.33) data.push([r,r*2]); | ||
var res = skmeans(data,3); | ||
console.log(data); | ||
console.log(res); | ||
console.log(res.it,res.centroids); |
const skmeans = require("../main.js"); | ||
var data = []; | ||
for(let i=0;i<20;i++) { | ||
for(let i=0;i<2000;i++) { | ||
var r = Math.random(); | ||
@@ -11,4 +11,3 @@ if(r>=0 && r<0.33) data.push(r); | ||
var res = skmeans(data,3); | ||
console.log(data); | ||
console.log(res); | ||
var res = skmeans(data,3,[1,5,9]); | ||
console.log(res.it,res.centroids); |
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
26189
419