Comparing version 0.10.0 to 0.10.1
"use strict"; | ||
(function e(t, n, r) { | ||
function s(o, u) { | ||
if (!n[o]) { | ||
if (!t[o]) { | ||
var a = typeof require == "function" && require;if (!u && a) return a(o, !0);if (i) return i(o, !0);var f = new Error("Cannot find module '" + o + "'");throw f.code = "MODULE_NOT_FOUND", f; | ||
}var l = n[o] = { exports: {} };t[o][0].call(l.exports, function (e) { | ||
var n = t[o][1][e];return s(n ? n : e); | ||
}, l, l.exports, e, t, n, r); | ||
}return n[o].exports; | ||
}var i = typeof require == "function" && require;for (var o = 0; o < r.length; o++) { | ||
s(r[o]); | ||
}return s; | ||
})({ 1: [function (require, module, exports) { | ||
"use strict"; | ||
(function () { | ||
function r(e, n, t) { | ||
function o(i, f) { | ||
if (!n[i]) { | ||
if (!e[i]) { | ||
var c = "function" == typeof require && require; | ||
if (!f && c) return c(i, !0); | ||
if (u) return u(i, !0); | ||
var a = new Error("Cannot find module '" + i + "'"); | ||
throw a.code = "MODULE_NOT_FOUND", a; | ||
} | ||
(function () { | ||
var root = this; | ||
var previous_skmeans = root.skmeans; | ||
var skmeans = require('./main.js'); | ||
var p = n[i] = { | ||
exports: {} | ||
}; | ||
e[i][0].call(p.exports, function (r) { | ||
var n = e[i][1][r]; | ||
return o(n || r); | ||
}, p, p.exports, r, e, n, t); | ||
} | ||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports = module.exports = skmeans; | ||
} | ||
exports.skmeans = skmeans; | ||
} | ||
return n[i].exports; | ||
} | ||
if (typeof window !== 'undefined') { | ||
window.skmeans = skmeans; | ||
} | ||
}).call(this); | ||
}, { "./main.js": 4 }], 2: [function (require, module, exports) { | ||
module.exports = { | ||
/** | ||
* Euclidean distance | ||
*/ | ||
eudist: function eudist(v1, v2, sqrt) { | ||
var len = v1.length; | ||
var sum = 0; | ||
for (var u = "function" == typeof require && require, i = 0; i < t.length; i++) { | ||
o(t[i]); | ||
} | ||
for (var i = 0; i < len; i++) { | ||
var d = (v1[i] || 0) - (v2[i] || 0); | ||
sum += d * d; | ||
} | ||
// Square root not really needed | ||
return sqrt ? Math.sqrt(sum) : sum; | ||
}, | ||
mandist: function mandist(v1, v2, sqrt) { | ||
var len = v1.length; | ||
var sum = 0; | ||
return o; | ||
} | ||
for (var i = 0; i < len; i++) { | ||
sum += Math.abs((v1[i] || 0) - (v2[i] || 0)); | ||
} | ||
return r; | ||
})()({ | ||
1: [function (require, module, exports) { | ||
"use strict"; | ||
// Square root not really needed | ||
return sqrt ? Math.sqrt(sum) : sum; | ||
}, | ||
(function () { | ||
var root = this; | ||
var previous_skmeans = root.skmeans; | ||
var skmeans = require('./main.js'); | ||
/** | ||
* Unidimensional distance | ||
*/ | ||
dist: function dist(v1, v2, sqrt) { | ||
var d = Math.abs(v1 - v2); | ||
return sqrt ? d : d * d; | ||
} | ||
}; | ||
}, {}], 3: [function (require, module, exports) { | ||
var Distance = require("./distance.js"), | ||
eudist = Distance.eudist, | ||
dist = Distance.dist; | ||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports = module.exports = skmeans; | ||
} | ||
module.exports = { | ||
kmrand: function kmrand(data, k) { | ||
var map = {}, | ||
ks = [], | ||
t = k << 2; | ||
var len = data.length; | ||
var multi = data[0].length > 0; | ||
exports.skmeans = skmeans; | ||
} | ||
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; | ||
ks.push(d); | ||
} | ||
} | ||
if (typeof window !== 'undefined') { | ||
window.skmeans = skmeans; | ||
} | ||
}).call(this); | ||
}, { | ||
"./main.js": 4 | ||
}], | ||
2: [function (require, module, exports) { | ||
module.exports = { | ||
/** | ||
* Euclidean distance | ||
*/ | ||
eudist: function eudist(v1, v2, sqrt) { | ||
var len = v1.length; | ||
var sum = 0; | ||
if (ks.length < k) throw new Error("Error initializating clusters");else return ks; | ||
}, | ||
for (var i = 0; i < len; i++) { | ||
var d = (v1[i] || 0) - (v2[i] || 0); | ||
sum += d * d; | ||
} // Square root not really needed | ||
/** | ||
* K-means++ initial centroid selection | ||
*/ | ||
kmpp: function kmpp(data, k) { | ||
var distance = data[0].length ? eudist : dist; | ||
var ks = [], | ||
len = data.length; | ||
var multi = data[0].length > 0; | ||
var map = {}; | ||
return sqrt ? Math.sqrt(sum) : sum; | ||
}, | ||
mandist: function mandist(v1, v2, sqrt) { | ||
var len = v1.length; | ||
var sum = 0; | ||
// First random centroid | ||
var c = data[Math.floor(Math.random() * len)]; | ||
var key = multi ? c.join("_") : "" + c; | ||
ks.push(c); | ||
map[key] = true; | ||
for (var i = 0; i < len; i++) { | ||
sum += Math.abs((v1[i] || 0) - (v2[i] || 0)); | ||
} // Square root not really needed | ||
// Retrieve next centroids | ||
while (ks.length < k) { | ||
// Min Distances between current centroids and data points | ||
var dists = [], | ||
lk = ks.length; | ||
var dsum = 0, | ||
prs = []; | ||
for (var i = 0; i < len; i++) { | ||
var min = Infinity; | ||
for (var j = 0; j < lk; j++) { | ||
var _dist = distance(data[i], ks[j]); | ||
if (_dist <= min) min = _dist; | ||
} | ||
dists[i] = min; | ||
} | ||
return sqrt ? Math.sqrt(sum) : sum; | ||
}, | ||
// Sum all min distances | ||
for (var _i = 0; _i < len; _i++) { | ||
dsum += dists[_i]; | ||
} | ||
/** | ||
* Unidimensional distance | ||
*/ | ||
dist: function dist(v1, v2, sqrt) { | ||
var d = Math.abs(v1 - v2); | ||
return sqrt ? d : d * d; | ||
} | ||
}; | ||
}, {}], | ||
3: [function (require, module, exports) { | ||
var Distance = require("./distance.js"), | ||
eudist = Distance.eudist, | ||
dist = Distance.dist; | ||
// Probabilities and cummulative prob (cumsum) | ||
for (var _i2 = 0; _i2 < len; _i2++) { | ||
prs[_i2] = { i: _i2, v: data[_i2], pr: dists[_i2] / dsum, cs: 0 }; | ||
} | ||
module.exports = { | ||
kmrand: function kmrand(data, k) { | ||
var map = {}, | ||
ks = [], | ||
t = k << 2; | ||
var len = data.length; | ||
var multi = data[0].length > 0; | ||
// Sort Probabilities | ||
prs.sort(function (a, b) { | ||
return a.pr - b.pr; | ||
}); | ||
while (ks.length < k && t-- > 0) { | ||
var d = data[Math.floor(Math.random() * len)]; | ||
var key = multi ? d.join("_") : "".concat(d); | ||
// Cummulative Probabilities | ||
prs[0].cs = prs[0].pr; | ||
for (var _i3 = 1; _i3 < len; _i3++) { | ||
prs[_i3].cs = prs[_i3 - 1].cs + prs[_i3].pr; | ||
} | ||
if (!map[key]) { | ||
map[key] = true; | ||
ks.push(d); | ||
} | ||
} | ||
// Randomize | ||
var rnd = Math.random(); | ||
if (ks.length < k) throw new Error("Error initializating clusters");else return ks; | ||
}, | ||
// Gets only the items whose cumsum >= rnd | ||
var idx = 0; | ||
while (idx < len - 1 && prs[idx++].cs < rnd) {} | ||
ks.push(prs[idx - 1].v); | ||
/* | ||
let done = false; | ||
while(!done) { | ||
// this is our new centroid | ||
c = prs[idx-1].v | ||
key = multi? c.join("_") : `${c}`; | ||
if(!map[key]) { | ||
map[key] = true; | ||
ks.push(c); | ||
done = true; | ||
} | ||
else { | ||
idx++; | ||
} | ||
} | ||
/** | ||
* K-means++ initial centroid selection | ||
*/ | ||
kmpp: function kmpp(data, k) { | ||
var distance = data[0].length ? eudist : dist; | ||
var ks = [], | ||
len = data.length; | ||
var multi = data[0].length > 0; | ||
var map = {}; // First random centroid | ||
var c = data[Math.floor(Math.random() * len)]; | ||
var key = multi ? c.join("_") : "".concat(c); | ||
ks.push(c); | ||
map[key] = true; // Retrieve next centroids | ||
while (ks.length < k) { | ||
// Min Distances between current centroids and data points | ||
var dists = [], | ||
lk = ks.length; | ||
var dsum = 0, | ||
prs = []; | ||
for (var i = 0; i < len; i++) { | ||
var min = Infinity; | ||
for (var j = 0; j < lk; j++) { | ||
var _dist = distance(data[i], ks[j]); | ||
if (_dist <= min) min = _dist; | ||
} | ||
dists[i] = min; | ||
} // Sum all min distances | ||
for (var _i = 0; _i < len; _i++) { | ||
dsum += dists[_i]; | ||
} // Probabilities and cummulative prob (cumsum) | ||
for (var _i2 = 0; _i2 < len; _i2++) { | ||
prs[_i2] = { | ||
i: _i2, | ||
v: data[_i2], | ||
pr: dists[_i2] / dsum, | ||
cs: 0 | ||
}; | ||
} // Sort Probabilities | ||
prs.sort(function (a, b) { | ||
return a.pr - b.pr; | ||
}); // Cummulative Probabilities | ||
prs[0].cs = prs[0].pr; | ||
for (var _i3 = 1; _i3 < len; _i3++) { | ||
prs[_i3].cs = prs[_i3 - 1].cs + prs[_i3].pr; | ||
} // Randomize | ||
var rnd = Math.random(); // Gets only the items whose cumsum >= rnd | ||
var idx = 0; | ||
while (idx < len - 1 && prs[idx++].cs < rnd) { | ||
; | ||
} | ||
ks.push(prs[idx - 1].v); | ||
/* | ||
let done = false; | ||
while(!done) { | ||
// this is our new centroid | ||
c = prs[idx-1].v | ||
key = multi? c.join("_") : `${c}`; | ||
if(!map[key]) { | ||
map[key] = true; | ||
ks.push(c); | ||
done = true; | ||
} | ||
else { | ||
idx++; | ||
} | ||
} | ||
*/ | ||
} | ||
return ks; | ||
} | ||
}; | ||
}, { | ||
"./distance.js": 2 | ||
}], | ||
4: [function (require, module, exports) { | ||
/*jshint esversion: 6 */ | ||
var Distance = require("./distance.js"), | ||
ClusterInit = require("./kinit.js"), | ||
eudist = Distance.eudist, | ||
mandist = Distance.mandist, | ||
dist = Distance.dist, | ||
kmrand = ClusterInit.kmrand, | ||
kmpp = ClusterInit.kmpp; | ||
var MAX = 10000; | ||
/** | ||
* Inits an array with values | ||
*/ | ||
} | ||
return ks; | ||
} | ||
}; | ||
}, { "./distance.js": 2 }], 4: [function (require, module, exports) { | ||
/*jshint esversion: 6 */ | ||
function init(len, val, v) { | ||
v = v || []; | ||
var Distance = require("./distance.js"), | ||
ClusterInit = require("./kinit.js"), | ||
eudist = Distance.eudist, | ||
mandist = Distance.mandist, | ||
dist = Distance.dist, | ||
kmrand = ClusterInit.kmrand, | ||
kmpp = ClusterInit.kmpp; | ||
for (var i = 0; i < len; i++) { | ||
v[i] = val; | ||
} | ||
var MAX = 10000; | ||
return v; | ||
} | ||
/** | ||
* Inits an array with values | ||
*/ | ||
function init(len, val, v) { | ||
v = v || []; | ||
for (var i = 0; i < len; i++) { | ||
v[i] = val; | ||
}return v; | ||
} | ||
function skmeans(data, k, initial, maxit, fndist) { | ||
var ks = [], | ||
old = [], | ||
idxs = [], | ||
dist = []; | ||
var conv = false, | ||
it = maxit || MAX; | ||
var len = data.length, | ||
vlen = data[0].length, | ||
multi = vlen > 0; | ||
var count = []; | ||
function skmeans(data, k, initial, maxit, fndist) { | ||
var ks = [], | ||
old = [], | ||
idxs = [], | ||
dist = []; | ||
var conv = false, | ||
it = maxit || MAX; | ||
var len = data.length, | ||
vlen = data[0].length, | ||
multi = vlen > 0; | ||
var count = []; | ||
if (!initial) { | ||
var _idxs = {}; | ||
if (!initial) { | ||
var _idxs = {}; | ||
while (ks.length < k) { | ||
var idx = Math.floor(Math.random() * len); | ||
if (!_idxs[idx]) { | ||
_idxs[idx] = true; | ||
ks.push(data[idx]); | ||
} | ||
} | ||
} else if (initial == "kmrand") { | ||
ks = kmrand(data, k); | ||
} else if (initial == "kmpp") { | ||
ks = kmpp(data, k); | ||
} else { | ||
ks = initial; | ||
} | ||
while (ks.length < k) { | ||
var idx = Math.floor(Math.random() * len); | ||
do { | ||
// Reset k count | ||
init(k, 0, count); | ||
if (!_idxs[idx]) { | ||
_idxs[idx] = true; | ||
ks.push(data[idx]); | ||
} | ||
} | ||
} else if (initial == "kmrand") { | ||
ks = kmrand(data, k); | ||
} else if (initial == "kmpp") { | ||
ks = kmpp(data, k); | ||
} else { | ||
ks = initial; | ||
} | ||
// For each value in data, find the nearest centroid | ||
for (var i = 0; i < len; i++) { | ||
var min = Infinity, | ||
_idx = 0; | ||
for (var j = 0; j < k; j++) { | ||
// Custom, Multidimensional or unidimensional | ||
var dist = fndist ? fndist(data[i], ks[k]) : multi ? eudist(data[i], ks[j]) : Math.abs(data[i] - ks[j]); | ||
do { | ||
// Reset k count | ||
init(k, 0, count); // For each value in data, find the nearest centroid | ||
if (dist <= min) { | ||
min = dist; | ||
_idx = j; | ||
} | ||
} | ||
idxs[i] = _idx; // Index of the selected centroid for that value | ||
count[_idx]++; // Number of values for this centroid | ||
} | ||
for (var i = 0; i < len; i++) { | ||
var min = Infinity, | ||
_idx = 0; | ||
// Recalculate centroids | ||
var sum = [], | ||
old = [], | ||
dif = 0; | ||
for (var _j = 0; _j < k; _j++) { | ||
// Multidimensional or unidimensional | ||
sum[_j] = multi ? init(vlen, 0, sum[_j]) : 0; | ||
old[_j] = ks[_j]; | ||
} | ||
for (var j = 0; j < k; j++) { | ||
// Custom, Multidimensional or unidimensional | ||
var dist = fndist ? fndist(data[i], ks[k]) : multi ? eudist(data[i], ks[j]) : Math.abs(data[i] - ks[j]); | ||
// If multidimensional | ||
if (multi) { | ||
for (var _j2 = 0; _j2 < k; _j2++) { | ||
ks[_j2] = []; | ||
} // Sum values and count for each centroid | ||
for (var _i4 = 0; _i4 < len; _i4++) { | ||
var _idx2 = idxs[_i4], | ||
// Centroid for that item | ||
vsum = sum[_idx2], | ||
// Sum values for this centroid | ||
vect = data[_i4]; // Current vector | ||
if (dist <= min) { | ||
min = dist; | ||
_idx = j; | ||
} | ||
} | ||
// Accumulate value on the centroid for current vector | ||
for (var h = 0; h < vlen; h++) { | ||
vsum[h] += vect[h]; | ||
} | ||
} | ||
// Calculate the average for each centroid | ||
conv = true; | ||
for (var _j3 = 0; _j3 < k; _j3++) { | ||
var ksj = ks[_j3], | ||
// Current centroid | ||
sumj = sum[_j3], | ||
// Accumulated centroid values | ||
oldj = old[_j3], | ||
// Old centroid value | ||
cj = count[_j3]; // Number of elements for this centroid | ||
idxs[i] = _idx; // Index of the selected centroid for that value | ||
// New average | ||
for (var _h = 0; _h < vlen; _h++) { | ||
ksj[_h] = sumj[_h] / cj || 0; // New centroid | ||
} | ||
count[_idx]++; // Number of values for this centroid | ||
} // Recalculate centroids | ||
// Find if centroids have moved | ||
if (conv) { | ||
for (var _h2 = 0; _h2 < vlen; _h2++) { | ||
if (oldj[_h2] != ksj[_h2]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// If unidimensional | ||
else { | ||
// Sum values and count for each centroid | ||
for (var _i5 = 0; _i5 < len; _i5++) { | ||
var _idx3 = idxs[_i5]; | ||
sum[_idx3] += data[_i5]; | ||
} | ||
// Calculate the average for each centroid | ||
for (var _j4 = 0; _j4 < k; _j4++) { | ||
ks[_j4] = sum[_j4] / count[_j4] || 0; // New centroid | ||
} | ||
// Find if centroids have moved | ||
conv = true; | ||
for (var _j5 = 0; _j5 < k; _j5++) { | ||
if (old[_j5] != ks[_j5]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
conv = conv || --it <= 0; | ||
} while (!conv); | ||
var sum = [], | ||
old = [], | ||
dif = 0; | ||
return { | ||
it: MAX - it, | ||
k: k, | ||
idxs: idxs, | ||
centroids: ks | ||
}; | ||
} | ||
for (var _j = 0; _j < k; _j++) { | ||
// Multidimensional or unidimensional | ||
sum[_j] = multi ? init(vlen, 0, sum[_j]) : 0; | ||
old[_j] = ks[_j]; | ||
} // If multidimensional | ||
module.exports = skmeans; | ||
}, { "./distance.js": 2, "./kinit.js": 3 }] }, {}, [1]); | ||
if (multi) { | ||
for (var _j2 = 0; _j2 < k; _j2++) { | ||
ks[_j2] = []; | ||
} // Sum values and count for each centroid | ||
for (var _i4 = 0; _i4 < len; _i4++) { | ||
var _idx2 = idxs[_i4], | ||
// Centroid for that item | ||
vsum = sum[_idx2], | ||
// Sum values for this centroid | ||
vect = data[_i4]; // Current vector | ||
// Accumulate value on the centroid for current vector | ||
for (var h = 0; h < vlen; h++) { | ||
vsum[h] += vect[h]; | ||
} | ||
} // Calculate the average for each centroid | ||
conv = true; | ||
for (var _j3 = 0; _j3 < k; _j3++) { | ||
var ksj = ks[_j3], | ||
// Current centroid | ||
sumj = sum[_j3], | ||
// Accumulated centroid values | ||
oldj = old[_j3], | ||
// Old centroid value | ||
cj = count[_j3]; // Number of elements for this centroid | ||
// New average | ||
for (var _h = 0; _h < vlen; _h++) { | ||
ksj[_h] = sumj[_h] / cj || 0; // New centroid | ||
} // Find if centroids have moved | ||
if (conv) { | ||
for (var _h2 = 0; _h2 < vlen; _h2++) { | ||
if (oldj[_h2] != ksj[_h2]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} // If unidimensional | ||
else { | ||
// Sum values and count for each centroid | ||
for (var _i5 = 0; _i5 < len; _i5++) { | ||
var _idx3 = idxs[_i5]; | ||
sum[_idx3] += data[_i5]; | ||
} // Calculate the average for each centroid | ||
for (var _j4 = 0; _j4 < k; _j4++) { | ||
ks[_j4] = sum[_j4] / count[_j4] || 0; // New centroid | ||
} // Find if centroids have moved | ||
conv = true; | ||
for (var _j5 = 0; _j5 < k; _j5++) { | ||
if (old[_j5] != ks[_j5]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
conv = conv || --it <= 0; | ||
} while (!conv); | ||
return { | ||
it: MAX - it, | ||
k: k, | ||
idxs: idxs, | ||
centroids: ks | ||
}; | ||
} | ||
module.exports = skmeans; | ||
}, { | ||
"./distance.js": 2, | ||
"./kinit.js": 3 | ||
}] | ||
}, {}, [1]); | ||
//# sourceMappingURL=skmeans.js.map |
@@ -1,3 +0,3 @@ | ||
/*! skmeans 2019-03-03 */ | ||
/*! skmeans 2019-04-13 */ | ||
"use strict";!function r(t,n,o){function a(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 a(n||r)},v,v.exports,r,t,n,o)}return n[i].exports}for(var e="function"==typeof require&&require,i=0;i<o.length;i++)a(o[i]);return a}({1:[function(r,t,n){(function(){this.skmeans;var o=r("./main.js");void 0!==n&&(void 0!==t&&t.exports&&(n=t.exports=o),n.skmeans=o),"undefined"!=typeof window&&(window.skmeans=o)}).call(this)},{"./main.js":4}],2:[function(r,t,n){t.exports={eudist:function(r,t,n){for(var o=r.length,a=0,e=0;e<o;e++){var i=(r[e]||0)-(t[e]||0);a+=i*i}return n?Math.sqrt(a):a},mandist:function(r,t,n){for(var o=r.length,a=0,e=0;e<o;e++)a+=Math.abs((r[e]||0)-(t[e]||0));return n?Math.sqrt(a):a},dist:function(r,t,n){var o=Math.abs(r-t);return n?o:o*o}}},{}],3:[function(r,t,n){var o=r("./distance.js"),a=o.eudist,e=o.dist;t.exports={kmrand:function(r,t){for(var n={},o=[],a=t<<2,e=r.length,i=r[0].length>0;o.length<t&&a-- >0;){var f=r[Math.floor(Math.random()*e)],s=i?f.join("_"):""+f;n[s]||(n[s]=!0,o.push(f))}if(o.length<t)throw new Error("Error initializating clusters");return o},kmpp:function(r,t){var n=r[0].length?a:e,o=[],i=r.length,f=r[0].length>0,s={},u=r[Math.floor(Math.random()*i)],v=f?u.join("_"):""+u;for(o.push(u),s[v]=!0;o.length<t;){for(var h=[],c=o.length,d=0,l=[],p=0;p<i;p++){for(var m=1/0,g=0;g<c;g++){var k=n(r[p],o[g]);k<=m&&(m=k)}h[p]=m}for(var M=0;M<i;M++)d+=h[M];for(var j=0;j<i;j++)l[j]={i:j,v:r[j],pr:h[j]/d,cs:0};l.sort(function(r,t){return r.pr-t.pr}),l[0].cs=l[0].pr;for(var x=1;x<i;x++)l[x].cs=l[x-1].cs+l[x].pr;for(var w=Math.random(),q=0;q<i-1&&l[q++].cs<w;);o.push(l[q-1].v)}return o}}},{"./distance.js":2}],4:[function(r,t,n){function o(r,t,n){n=n||[];for(var o=0;o<r;o++)n[o]=t;return n}var a=r("./distance.js"),e=r("./kinit.js"),i=a.eudist,f=(a.mandist,a.dist,e.kmrand),s=e.kmpp,u=1e4;t.exports=function(r,t,n,a,e){var v=[],h=[],c=[],d=[],l=!1,p=a||u,m=r.length,g=r[0].length,k=g>0,M=[];if(n)v="kmrand"==n?f(r,t):"kmpp"==n?s(r,t):n;else for(var j={};v.length<t;){var x=Math.floor(Math.random()*m);j[x]||(j[x]=!0,v.push(r[x]))}do{o(t,0,M);for(var w=0;w<m;w++){for(var q=1/0,b=0,E=0;E<t;E++)(d=e?e(r[w],v[t]):k?i(r[w],v[E]):Math.abs(r[w]-v[E]))<=q&&(q=d,b=E);c[w]=b,M[b]++}for(var _=[],h=[],y=0;y<t;y++)_[y]=k?o(g,0,_[y]):0,h[y]=v[y];if(k){for(var O=0;O<t;O++)v[O]=[];for(var D=0;D<m;D++)for(var N=_[c[D]],U=r[D],z=0;z<g;z++)N[z]+=U[z];l=!0;for(var C=0;C<t;C++){for(var F=v[C],L=_[C],T=h[C],A=M[C],B=0;B<g;B++)F[B]=L[B]/A||0;if(l)for(var G=0;G<g;G++)if(T[G]!=F[G]){l=!1;break}}}else{for(var H=0;H<m;H++)_[c[H]]+=r[H];for(var I=0;I<t;I++)v[I]=_[I]/M[I]||0;l=!0;for(var J=0;J<t;J++)if(h[J]!=v[J]){l=!1;break}}l=l||--p<=0}while(!l);return{it:u-p,k:t,idxs:c,centroids:v}}},{"./distance.js":2,"./kinit.js":3}]},{},[1]); | ||
"use strict";!function e(i,f,s){function u(t,r){if(!f[t]){if(!i[t]){var n="function"==typeof require&&require;if(!r&&n)return n(t,!0);if(v)return v(t,!0);var o=new Error("Cannot find module '"+t+"'");throw o.code="MODULE_NOT_FOUND",o}var a=f[t]={exports:{}};i[t][0].call(a.exports,function(r){return u(i[t][1][r]||r)},a,a.exports,e,i,f,s)}return f[t].exports}for(var v="function"==typeof require&&require,r=0;r<s.length;r++)u(s[r]);return u}({1:[function(t,n,o){(function(){this.skmeans;var r=t("./main.js");void 0!==o&&(void 0!==n&&n.exports&&(o=n.exports=r),o.skmeans=r),"undefined"!=typeof window&&(window.skmeans=r)}).call(this)},{"./main.js":4}],2:[function(r,t,n){t.exports={eudist:function(r,t,n){for(var o=r.length,a=0,e=0;e<o;e++){var i=(r[e]||0)-(t[e]||0);a+=i*i}return n?Math.sqrt(a):a},mandist:function(r,t,n){for(var o=r.length,a=0,e=0;e<o;e++)a+=Math.abs((r[e]||0)-(t[e]||0));return n?Math.sqrt(a):a},dist:function(r,t,n){var o=Math.abs(r-t);return n?o:o*o}}},{}],3:[function(r,t,n){var o=r("./distance.js"),j=o.eudist,x=o.dist;t.exports={kmrand:function(r,t){for(var n={},o=[],a=t<<2,e=r.length,i=0<r[0].length;o.length<t&&0<a--;){var f=r[Math.floor(Math.random()*e)],s=i?f.join("_"):"".concat(f);n[s]||(n[s]=!0,o.push(f))}if(o.length<t)throw new Error("Error initializating clusters");return o},kmpp:function(r,t){var n=r[0].length?j:x,o=[],a=r.length,e=0<r[0].length,i=r[Math.floor(Math.random()*a)];e?i.join("_"):"".concat(i);for(o.push(i);o.length<t;){for(var f=[],s=o.length,u=0,v=[],c=0;c<a;c++){for(var h=1/0,d=0;d<s;d++){var l=n(r[c],o[d]);l<=h&&(h=l)}f[c]=h}for(var p=0;p<a;p++)u+=f[p];for(var m=0;m<a;m++)v[m]={i:m,v:r[m],pr:f[m]/u,cs:0};v.sort(function(r,t){return r.pr-t.pr}),v[0].cs=v[0].pr;for(var g=1;g<a;g++)v[g].cs=v[g-1].cs+v[g].pr;for(var k=Math.random(),M=0;M<a-1&&v[M++].cs<k;);o.push(v[M-1].v)}return o}}},{"./distance.js":2}],4:[function(r,t,n){var o=r("./distance.js"),a=r("./kinit.js"),B=o.eudist,G=(o.mandist,o.dist,a.kmrand),H=a.kmpp;function I(r,t,n){n=n||[];for(var o=0;o<r;o++)n[o]=t;return n}t.exports=function(r,t,n,o,a){var e=[],i=[],f=[],s=[],u=!1,v=o||1e4,c=r.length,h=r[0].length,d=0<h,l=[];if(n)e="kmrand"==n?G(r,t):"kmpp"==n?H(r,t):n;else for(var p={};e.length<t;){var m=Math.floor(Math.random()*c);p[m]||(p[m]=!0,e.push(r[m]))}do{I(t,0,l);for(var g=0;g<c;g++){for(var k=1/0,M=0,j=0;j<t;j++)(s=a?a(r[g],e[t]):d?B(r[g],e[j]):Math.abs(r[g]-e[j]))<=k&&(k=s,M=j);l[f[g]=M]++}for(var x=[],w=(i=[],0);w<t;w++)x[w]=d?I(h,0,x[w]):0,i[w]=e[w];if(d){for(var q=0;q<t;q++)e[q]=[];for(var b=0;b<c;b++)for(var E=x[f[b]],_=r[b],y=0;y<h;y++)E[y]+=_[y];u=!0;for(var O=0;O<t;O++){for(var D=e[O],N=x[O],U=i[O],z=l[O],C=0;C<h;C++)D[C]=N[C]/z||0;if(u)for(var F=0;F<h;F++)if(U[F]!=D[F]){u=!1;break}}}else{for(var L=0;L<c;L++)x[f[L]]+=r[L];for(var T=0;T<t;T++)e[T]=x[T]/l[T]||0;u=!0;for(var A=0;A<t;A++)if(i[A]!=e[A]){u=!1;break}}u=u||--v<=0}while(!u);return{it:1e4-v,k:t,idxs:f,centroids:e}}},{"./distance.js":2,"./kinit.js":3}]},{},[1]); |
@@ -6,2 +6,3 @@ "use strict"; | ||
var previous_skmeans = root.skmeans; | ||
var skmeans = require('./main.js'); | ||
@@ -13,2 +14,3 @@ | ||
} | ||
exports.skmeans = skmeans; | ||
@@ -20,3 +22,3 @@ } | ||
} | ||
}).call(undefined); | ||
}).call(void 0); | ||
//# sourceMappingURL=browser.js.map |
"use strict"; | ||
module.exports = { | ||
/** | ||
* Euclidean distance | ||
*/ | ||
eudist: function eudist(v1, v2, sqrt) { | ||
var len = v1.length; | ||
var sum = 0; | ||
/** | ||
* Euclidean distance | ||
*/ | ||
eudist: function eudist(v1, v2, sqrt) { | ||
var len = v1.length; | ||
var sum = 0; | ||
for (var i = 0; i < len; i++) { | ||
var d = (v1[i] || 0) - (v2[i] || 0); | ||
sum += d * d; | ||
} | ||
// Square root not really needed | ||
return sqrt ? Math.sqrt(sum) : sum; | ||
}, | ||
mandist: function mandist(v1, v2, sqrt) { | ||
var len = v1.length; | ||
var sum = 0; | ||
for (var i = 0; i < len; i++) { | ||
var d = (v1[i] || 0) - (v2[i] || 0); | ||
sum += d * d; | ||
} // Square root not really needed | ||
for (var i = 0; i < len; i++) { | ||
sum += Math.abs((v1[i] || 0) - (v2[i] || 0)); | ||
} | ||
// Square root not really needed | ||
return sqrt ? Math.sqrt(sum) : sum; | ||
}, | ||
return sqrt ? Math.sqrt(sum) : sum; | ||
}, | ||
mandist: function mandist(v1, v2, sqrt) { | ||
var len = v1.length; | ||
var sum = 0; | ||
for (var i = 0; i < len; i++) { | ||
sum += Math.abs((v1[i] || 0) - (v2[i] || 0)); | ||
} // Square root not really needed | ||
/** | ||
* Unidimensional distance | ||
*/ | ||
dist: function dist(v1, v2, sqrt) { | ||
var d = Math.abs(v1 - v2); | ||
return sqrt ? d : d * d; | ||
} | ||
return sqrt ? Math.sqrt(sum) : sum; | ||
}, | ||
/** | ||
* Unidimensional distance | ||
*/ | ||
dist: function dist(v1, v2, sqrt) { | ||
var d = Math.abs(v1 - v2); | ||
return sqrt ? d : d * d; | ||
} | ||
}; | ||
//# sourceMappingURL=distance.js.map |
@@ -1,49 +0,48 @@ | ||
'use strict'; | ||
"use strict"; | ||
module.exports = function (grunt) { | ||
require('load-grunt-tasks')(grunt); | ||
require('load-grunt-tasks')(grunt); // Project configuration. | ||
// Project configuration. | ||
grunt.initConfig({ | ||
pkg: grunt.file.readJSON('package.json'), | ||
browserify: { | ||
dist: { | ||
watch: true, | ||
keepAlive: true, | ||
files: { | ||
'dist/browser/skmeans.js': ['browser.js'] | ||
} | ||
} | ||
}, | ||
babel: { | ||
options: { | ||
sourceMap: true, | ||
presets: ['es2015'] | ||
}, | ||
dist: { | ||
files: [{ | ||
expand: true, | ||
src: ['*.js'], | ||
dest: 'dist/node', | ||
ext: '.js' | ||
}, { | ||
'dist/browser/skmeans.js': ['dist/browser/skmeans.js'] | ||
}] | ||
} | ||
}, | ||
uglify: { | ||
options: { | ||
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n' | ||
}, | ||
dist: { | ||
files: { | ||
'dist/browser/skmeans.min.js': ['dist/browser/skmeans.js'] | ||
} | ||
} | ||
}, | ||
clean: ['dist/browser/*.js', 'dist/browser/*.map'] | ||
}); | ||
grunt.registerTask('default', ['browserify', 'babel', 'uglify']); | ||
grunt.initConfig({ | ||
pkg: grunt.file.readJSON('package.json'), | ||
browserify: { | ||
dist: { | ||
watch: true, | ||
keepAlive: true, | ||
files: { | ||
'dist/browser/skmeans.js': ['browser.js'] | ||
} | ||
} | ||
}, | ||
babel: { | ||
options: { | ||
sourceMap: true, | ||
presets: ['@babel/preset-env'] | ||
}, | ||
dist: { | ||
files: [{ | ||
expand: true, | ||
src: ['*.js'], | ||
dest: 'dist/node', | ||
ext: '.js' | ||
}, { | ||
'dist/browser/skmeans.js': ['dist/browser/skmeans.js'] | ||
}] | ||
} | ||
}, | ||
uglify: { | ||
options: { | ||
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n' | ||
}, | ||
dist: { | ||
files: { | ||
'dist/browser/skmeans.min.js': ['dist/browser/skmeans.js'] | ||
} | ||
} | ||
}, | ||
clean: ['dist/browser/*.js', 'dist/browser/*.map'] | ||
}); | ||
grunt.registerTask('default', ['browserify', 'babel', 'uglify']); | ||
}; | ||
//# sourceMappingURL=Gruntfile.js.map |
@@ -8,104 +8,113 @@ "use strict"; | ||
module.exports = { | ||
kmrand: function kmrand(data, k) { | ||
var map = {}, | ||
ks = [], | ||
t = k << 2; | ||
var len = data.length; | ||
var multi = data[0].length > 0; | ||
kmrand: function kmrand(data, k) { | ||
var map = {}, | ||
ks = [], | ||
t = k << 2; | ||
var len = data.length; | ||
var multi = data[0].length > 0; | ||
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; | ||
ks.push(d); | ||
} | ||
} | ||
while (ks.length < k && t-- > 0) { | ||
var d = data[Math.floor(Math.random() * len)]; | ||
var key = multi ? d.join("_") : "".concat(d); | ||
if (ks.length < k) throw new Error("Error initializating clusters");else return ks; | ||
}, | ||
if (!map[key]) { | ||
map[key] = true; | ||
ks.push(d); | ||
} | ||
} | ||
if (ks.length < k) throw new Error("Error initializating clusters");else return ks; | ||
}, | ||
/** | ||
* K-means++ initial centroid selection | ||
*/ | ||
kmpp: function kmpp(data, k) { | ||
var distance = data[0].length ? eudist : dist; | ||
var ks = [], | ||
len = data.length; | ||
var multi = data[0].length > 0; | ||
var map = {}; | ||
/** | ||
* K-means++ initial centroid selection | ||
*/ | ||
kmpp: function kmpp(data, k) { | ||
var distance = data[0].length ? eudist : dist; | ||
var ks = [], | ||
len = data.length; | ||
var multi = data[0].length > 0; | ||
var map = {}; // First random centroid | ||
// First random centroid | ||
var c = data[Math.floor(Math.random() * len)]; | ||
var key = multi ? c.join("_") : "" + c; | ||
ks.push(c); | ||
map[key] = true; | ||
var c = data[Math.floor(Math.random() * len)]; | ||
var key = multi ? c.join("_") : "".concat(c); | ||
ks.push(c); | ||
map[key] = true; // Retrieve next centroids | ||
// Retrieve next centroids | ||
while (ks.length < k) { | ||
// Min Distances between current centroids and data points | ||
var dists = [], | ||
lk = ks.length; | ||
var dsum = 0, | ||
prs = []; | ||
while (ks.length < k) { | ||
// Min Distances between current centroids and data points | ||
var dists = [], | ||
lk = ks.length; | ||
var dsum = 0, | ||
prs = []; | ||
for (var i = 0; i < len; i++) { | ||
var min = Infinity; | ||
for (var j = 0; j < lk; j++) { | ||
var _dist = distance(data[i], ks[j]); | ||
if (_dist <= min) min = _dist; | ||
} | ||
dists[i] = min; | ||
} | ||
for (var i = 0; i < len; i++) { | ||
var min = Infinity; | ||
// Sum all min distances | ||
for (var _i = 0; _i < len; _i++) { | ||
dsum += dists[_i]; | ||
} | ||
for (var j = 0; j < lk; j++) { | ||
var _dist = distance(data[i], ks[j]); | ||
// Probabilities and cummulative prob (cumsum) | ||
for (var _i2 = 0; _i2 < len; _i2++) { | ||
prs[_i2] = { i: _i2, v: data[_i2], pr: dists[_i2] / dsum, cs: 0 }; | ||
} | ||
if (_dist <= min) min = _dist; | ||
} | ||
// Sort Probabilities | ||
prs.sort(function (a, b) { | ||
return a.pr - b.pr; | ||
}); | ||
dists[i] = min; | ||
} // Sum all min distances | ||
// Cummulative Probabilities | ||
prs[0].cs = prs[0].pr; | ||
for (var _i3 = 1; _i3 < len; _i3++) { | ||
prs[_i3].cs = prs[_i3 - 1].cs + prs[_i3].pr; | ||
} | ||
// Randomize | ||
var rnd = Math.random(); | ||
for (var _i = 0; _i < len; _i++) { | ||
dsum += dists[_i]; | ||
} // Probabilities and cummulative prob (cumsum) | ||
// Gets only the items whose cumsum >= rnd | ||
var idx = 0; | ||
while (idx < len - 1 && prs[idx++].cs < rnd) {} | ||
ks.push(prs[idx - 1].v); | ||
/* | ||
let done = false; | ||
while(!done) { | ||
// this is our new centroid | ||
c = prs[idx-1].v | ||
key = multi? c.join("_") : `${c}`; | ||
if(!map[key]) { | ||
map[key] = true; | ||
ks.push(c); | ||
done = true; | ||
} | ||
else { | ||
idx++; | ||
} | ||
} | ||
*/ | ||
} | ||
return ks; | ||
} | ||
for (var _i2 = 0; _i2 < len; _i2++) { | ||
prs[_i2] = { | ||
i: _i2, | ||
v: data[_i2], | ||
pr: dists[_i2] / dsum, | ||
cs: 0 | ||
}; | ||
} // Sort Probabilities | ||
prs.sort(function (a, b) { | ||
return a.pr - b.pr; | ||
}); // Cummulative Probabilities | ||
prs[0].cs = prs[0].pr; | ||
for (var _i3 = 1; _i3 < len; _i3++) { | ||
prs[_i3].cs = prs[_i3 - 1].cs + prs[_i3].pr; | ||
} // Randomize | ||
var rnd = Math.random(); // Gets only the items whose cumsum >= rnd | ||
var idx = 0; | ||
while (idx < len - 1 && prs[idx++].cs < rnd) { | ||
; | ||
} | ||
ks.push(prs[idx - 1].v); | ||
/* | ||
let done = false; | ||
while(!done) { | ||
// this is our new centroid | ||
c = prs[idx-1].v | ||
key = multi? c.join("_") : `${c}`; | ||
if(!map[key]) { | ||
map[key] = true; | ||
ks.push(c); | ||
done = true; | ||
} | ||
else { | ||
idx++; | ||
} | ||
} | ||
*/ | ||
} | ||
return ks; | ||
} | ||
}; | ||
//# sourceMappingURL=kinit.js.map |
"use strict"; | ||
/*jshint esversion: 6 */ | ||
var Distance = require("./distance.js"), | ||
@@ -14,147 +13,161 @@ ClusterInit = require("./kinit.js"), | ||
var MAX = 10000; | ||
/** | ||
* Inits an array with values | ||
*/ | ||
function init(len, val, v) { | ||
v = v || []; | ||
for (var i = 0; i < len; i++) { | ||
v[i] = val; | ||
}return v; | ||
v = v || []; | ||
for (var i = 0; i < len; i++) { | ||
v[i] = val; | ||
} | ||
return v; | ||
} | ||
function skmeans(data, k, initial, maxit, fndist) { | ||
var ks = [], | ||
old = [], | ||
idxs = [], | ||
dist = []; | ||
var conv = false, | ||
it = maxit || MAX; | ||
var len = data.length, | ||
vlen = data[0].length, | ||
multi = vlen > 0; | ||
var count = []; | ||
var ks = [], | ||
old = [], | ||
idxs = [], | ||
dist = []; | ||
var conv = false, | ||
it = maxit || MAX; | ||
var len = data.length, | ||
vlen = data[0].length, | ||
multi = vlen > 0; | ||
var count = []; | ||
if (!initial) { | ||
var _idxs = {}; | ||
while (ks.length < k) { | ||
var idx = Math.floor(Math.random() * len); | ||
if (!_idxs[idx]) { | ||
_idxs[idx] = true; | ||
ks.push(data[idx]); | ||
} | ||
} | ||
} else if (initial == "kmrand") { | ||
ks = kmrand(data, k); | ||
} else if (initial == "kmpp") { | ||
ks = kmpp(data, k); | ||
} else { | ||
ks = initial; | ||
} | ||
if (!initial) { | ||
var _idxs = {}; | ||
do { | ||
// Reset k count | ||
init(k, 0, count); | ||
while (ks.length < k) { | ||
var idx = Math.floor(Math.random() * len); | ||
// For each value in data, find the nearest centroid | ||
for (var i = 0; i < len; i++) { | ||
var min = Infinity, | ||
_idx = 0; | ||
for (var j = 0; j < k; j++) { | ||
// Custom, Multidimensional or unidimensional | ||
var dist = fndist ? fndist(data[i], ks[k]) : multi ? eudist(data[i], ks[j]) : Math.abs(data[i] - ks[j]); | ||
if (!_idxs[idx]) { | ||
_idxs[idx] = true; | ||
ks.push(data[idx]); | ||
} | ||
} | ||
} else if (initial == "kmrand") { | ||
ks = kmrand(data, k); | ||
} else if (initial == "kmpp") { | ||
ks = kmpp(data, k); | ||
} else { | ||
ks = initial; | ||
} | ||
if (dist <= min) { | ||
min = dist; | ||
_idx = j; | ||
} | ||
} | ||
idxs[i] = _idx; // Index of the selected centroid for that value | ||
count[_idx]++; // Number of values for this centroid | ||
} | ||
do { | ||
// Reset k count | ||
init(k, 0, count); // For each value in data, find the nearest centroid | ||
// Recalculate centroids | ||
var sum = [], | ||
old = [], | ||
dif = 0; | ||
for (var _j = 0; _j < k; _j++) { | ||
// Multidimensional or unidimensional | ||
sum[_j] = multi ? init(vlen, 0, sum[_j]) : 0; | ||
old[_j] = ks[_j]; | ||
} | ||
for (var i = 0; i < len; i++) { | ||
var min = Infinity, | ||
_idx = 0; | ||
// If multidimensional | ||
if (multi) { | ||
for (var _j2 = 0; _j2 < k; _j2++) { | ||
ks[_j2] = []; | ||
} // Sum values and count for each centroid | ||
for (var _i = 0; _i < len; _i++) { | ||
var _idx2 = idxs[_i], | ||
// Centroid for that item | ||
vsum = sum[_idx2], | ||
// Sum values for this centroid | ||
vect = data[_i]; // Current vector | ||
for (var j = 0; j < k; j++) { | ||
// Custom, Multidimensional or unidimensional | ||
var dist = fndist ? fndist(data[i], ks[k]) : multi ? eudist(data[i], ks[j]) : Math.abs(data[i] - ks[j]); | ||
// Accumulate value on the centroid for current vector | ||
for (var h = 0; h < vlen; h++) { | ||
vsum[h] += vect[h]; | ||
} | ||
} | ||
// Calculate the average for each centroid | ||
conv = true; | ||
for (var _j3 = 0; _j3 < k; _j3++) { | ||
var ksj = ks[_j3], | ||
// Current centroid | ||
sumj = sum[_j3], | ||
// Accumulated centroid values | ||
oldj = old[_j3], | ||
// Old centroid value | ||
cj = count[_j3]; // Number of elements for this centroid | ||
if (dist <= min) { | ||
min = dist; | ||
_idx = j; | ||
} | ||
} | ||
// New average | ||
for (var _h = 0; _h < vlen; _h++) { | ||
ksj[_h] = sumj[_h] / cj || 0; // New centroid | ||
} | ||
idxs[i] = _idx; // Index of the selected centroid for that value | ||
// Find if centroids have moved | ||
if (conv) { | ||
for (var _h2 = 0; _h2 < vlen; _h2++) { | ||
if (oldj[_h2] != ksj[_h2]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// If unidimensional | ||
else { | ||
// Sum values and count for each centroid | ||
for (var _i2 = 0; _i2 < len; _i2++) { | ||
var _idx3 = idxs[_i2]; | ||
sum[_idx3] += data[_i2]; | ||
} | ||
// Calculate the average for each centroid | ||
for (var _j4 = 0; _j4 < k; _j4++) { | ||
ks[_j4] = sum[_j4] / count[_j4] || 0; // New centroid | ||
} | ||
// Find if centroids have moved | ||
conv = true; | ||
for (var _j5 = 0; _j5 < k; _j5++) { | ||
if (old[_j5] != ks[_j5]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
count[_idx]++; // Number of values for this centroid | ||
} // Recalculate centroids | ||
conv = conv || --it <= 0; | ||
} while (!conv); | ||
return { | ||
it: MAX - it, | ||
k: k, | ||
idxs: idxs, | ||
centroids: ks | ||
}; | ||
var sum = [], | ||
old = [], | ||
dif = 0; | ||
for (var _j = 0; _j < k; _j++) { | ||
// Multidimensional or unidimensional | ||
sum[_j] = multi ? init(vlen, 0, sum[_j]) : 0; | ||
old[_j] = ks[_j]; | ||
} // If multidimensional | ||
if (multi) { | ||
for (var _j2 = 0; _j2 < k; _j2++) { | ||
ks[_j2] = []; | ||
} // Sum values and count for each centroid | ||
for (var _i = 0; _i < len; _i++) { | ||
var _idx2 = idxs[_i], | ||
// Centroid for that item | ||
vsum = sum[_idx2], | ||
// Sum values for this centroid | ||
vect = data[_i]; // Current vector | ||
// Accumulate value on the centroid for current vector | ||
for (var h = 0; h < vlen; h++) { | ||
vsum[h] += vect[h]; | ||
} | ||
} // Calculate the average for each centroid | ||
conv = true; | ||
for (var _j3 = 0; _j3 < k; _j3++) { | ||
var ksj = ks[_j3], | ||
// Current centroid | ||
sumj = sum[_j3], | ||
// Accumulated centroid values | ||
oldj = old[_j3], | ||
// Old centroid value | ||
cj = count[_j3]; // Number of elements for this centroid | ||
// New average | ||
for (var _h = 0; _h < vlen; _h++) { | ||
ksj[_h] = sumj[_h] / cj || 0; // New centroid | ||
} // Find if centroids have moved | ||
if (conv) { | ||
for (var _h2 = 0; _h2 < vlen; _h2++) { | ||
if (oldj[_h2] != ksj[_h2]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} // If unidimensional | ||
else { | ||
// Sum values and count for each centroid | ||
for (var _i2 = 0; _i2 < len; _i2++) { | ||
var _idx3 = idxs[_i2]; | ||
sum[_idx3] += data[_i2]; | ||
} // Calculate the average for each centroid | ||
for (var _j4 = 0; _j4 < k; _j4++) { | ||
ks[_j4] = sum[_j4] / count[_j4] || 0; // New centroid | ||
} // Find if centroids have moved | ||
conv = true; | ||
for (var _j5 = 0; _j5 < k; _j5++) { | ||
if (old[_j5] != ks[_j5]) { | ||
conv = false; | ||
break; | ||
} | ||
} | ||
} | ||
conv = conv || --it <= 0; | ||
} while (!conv); | ||
return { | ||
it: MAX - it, | ||
k: k, | ||
idxs: idxs, | ||
centroids: ks | ||
}; | ||
} | ||
@@ -161,0 +174,0 @@ |
@@ -19,3 +19,3 @@ module.exports = function(grunt) { | ||
sourceMap: true, | ||
presets: ['es2015'] | ||
presets: ['@babel/preset-env'] | ||
}, | ||
@@ -22,0 +22,0 @@ dist: { |
{ | ||
"name": "skmeans", | ||
"version": "0.10.0", | ||
"version": "0.10.1", | ||
"description": "Super fast simple k-means and k-means++ clustering for unidimiensional and multidimensional data. Works in node and browser", | ||
@@ -25,11 +25,11 @@ "author": "David Gómez Matarrodona <solzimer@gmail.com>", | ||
"devDependencies": { | ||
"babel-preset-es2015": "^6.24.1", | ||
"grunt": "^1.0.1", | ||
"grunt-babel": "^6.0.0", | ||
"grunt-browserify": "^5.0.0", | ||
"grunt-contrib-clean": "^1.1.0", | ||
"grunt-contrib-uglify": "^3.0.1", | ||
"load-grunt-tasks": "^3.5.2", | ||
"uglify": "^0.1.5" | ||
"@babel/core": "^7.4.3", | ||
"@babel/preset-env": "^7.4.3", | ||
"grunt": "^1.0.4", | ||
"grunt-babel": "^8.0.0", | ||
"grunt-browserify": "^5.3.0", | ||
"grunt-contrib-clean": "^2.0.0", | ||
"grunt-contrib-uglify": "^4.0.1", | ||
"load-grunt-tasks": "^4.0.0" | ||
} | ||
} |
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
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
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
100438
1127