-
Notifications
You must be signed in to change notification settings - Fork 0
/
KMeans.js
60 lines (60 loc) · 1.29 KB
/
KMeans.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
class KMeans{
constructor(){
this.k = 3;
this.points = [];
this.centroids = [];
this.setK();
}
addPoint(pt){
this.points.push(pt);
}
setK(k=3){
this.k = k;
this.centroids = [];
for(let i=0; i<this.k; i++){
let c = new Point(random(0, width), random(0, height));
this.centroids.push(c);
}
}
findCentroids(){
for(let p of this.points){
let closestInd, minDist=9999;
for(let i=0; i<this.centroids.length; i++){
let d = this.dist(p, this.centroids[i]);
if (d < minDist){
minDist = d;
closestInd = i;
}
}
p.cls = closestInd;
}
}
updateCentroids(){
let ctr = [];
let avg = [];
for(let i=0; i<this.k; i++){
ctr.push(0);
avg.push([0, 0]);
}
for(let p of this.points){
avg[p.cls][0] += p.x;
avg[p.cls][1] += p.y;
ctr[p.cls] += 1;
}
for(let i=0; i<this.k; i++){
this.centroids[i].x = avg[i][0] / ctr[i];
this.centroids[i].y = avg[i][1] / ctr[i];
}
}
showCentroids(){
strokeWeight(10);
for(let [i, c] of this.centroids.entries()){
let s = colors[i];
stroke(s);
point(c.x, c.y);
}
}
dist(pt1, pt2){
return Math.sqrt(Math.pow(pt1.x-pt2.x, 2) + Math.pow(pt1.y-pt2.y, 2));
}
}