-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_recalculateCentroids.js
84 lines (77 loc) · 2.38 KB
/
04_recalculateCentroids.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
function run(){
var k = 3;
var means = recalculateCentroids(clusters);
// graph data plus old means
var oldMeans = [{x:100,y:75},{x:175,y:300},{x:500,y:275}];
clearCanvas();
graphClusters(clusters);
graphMeans(oldMeans, 'gray');
// graph new means
graphMeans(means, 'pink', oldMeans);
// display means using text
var html = '';
means.forEach(function(point,i){
html += '{x:' + point.x +
',y:' + point.y + '},<br>';
});
document.getElementById('means').innerHTML = html;
}
// data points for kMeans
var data = [
{x:150, y:100, centroid:null},
{x:135, y:65, centroid:null},
{x:100, y:50, centroid:null},
{x:65, y:65, centroid:null},
{x:50, y:100, centroid:null},
{x:65, y:135, centroid:null},
{x:100, y:150, centroid:null},
{x:135, y:135, centroid:null},
{x:600, y:350, centroid:null},
{x:571, y:279, centroid:null},
{x:500, y:250, centroid:null},
{x:429, y:279, centroid:null},
{x:400, y:350, centroid:null},
{x:429, y:421, centroid:null},
{x:500, y:450, centroid:null},
{x:571, y:421, centroid:null},
{x:250, y:350, centroid:null},
{x:228, y:297, centroid:null},
{x:175, y:275, centroid:null},
{x:122, y:297, centroid:null},
{x:100, y:350, centroid:null},
{x:122, y:403, centroid:null},
{x:175, y:425, centroid:null},
{x:228, y:403, centroid:null}
];
var clusters = [
[
{x:150, y:100, centroid:0},
{x:135, y:65, centroid:0},
{x:100, y:50, centroid:0},
{x:65, y:65, centroid:0},
{x:50, y:100, centroid:0},
{x:65, y:135, centroid:0},
{x:100, y:150, centroid:0},
{x:135, y:135, centroid:0},
],
[
{x:250, y:350, centroid:1},
{x:228, y:297, centroid:1},
{x:175, y:275, centroid:1},
{x:122, y:297, centroid:1},
{x:100, y:350, centroid:1},
{x:122, y:403, centroid:1},
{x:175, y:425, centroid:1},
{x:228, y:403, centroid:1}
],
[
{x:600, y:350, centroid:2},
{x:571, y:279, centroid:2},
{x:500, y:250, centroid:2},
{x:429, y:279, centroid:2},
{x:400, y:350, centroid:2},
{x:429, y:421, centroid:2},
{x:500, y:450, centroid:2},
{x:571, y:421, centroid:2},
]
];