-
Notifications
You must be signed in to change notification settings - Fork 9
/
kinit.js
102 lines (87 loc) · 2.05 KB
/
kinit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const
Distance = require("./distance.js"),
eudist = Distance.eudist,
dist = Distance.dist;
module.exports = {
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) {
let d = data[Math.floor(Math.random()*len)];
let key = multi? d.join("_") : `${d}`;
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(data,k,fndist) {
var distance = fndist || (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("_") : `${c}`;
ks.push(c);
map[key] = true;
// Retrieve next centroids
while(ks.length<k) {
// Min Distances between current centroids and data points
let dists = [], lk = ks.length;
let dsum = 0, prs = [];
for(let i=0;i<len;i++) {
let min = Infinity;
for(let j=0;j<lk;j++) {
let dist = distance(data[i],ks[j]);
if(dist<=min) min = dist;
}
dists[i] = min;
}
// Sum all min distances
for(let i=0;i<len;i++) {
dsum += dists[i]
}
// Probabilities and cummulative prob (cumsum)
for(let i=0;i<len;i++) {
prs[i] = {i:i, v:data[i], pr:dists[i]/dsum, cs:0}
}
// Sort Probabilities
prs.sort((a,b)=>a.pr-b.pr);
// Cummulative Probabilities
prs[0].cs = prs[0].pr;
for(let i=1;i<len;i++) {
prs[i].cs = prs[i-1].cs + prs[i].pr;
}
// Randomize
let rnd = Math.random();
// Gets only the items whose cumsum >= rnd
let 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;
}
}