-
Notifications
You must be signed in to change notification settings - Fork 0
/
kMeansClustering.js
275 lines (214 loc) · 5.15 KB
/
kMeansClustering.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var canvas;
var shape;
var height = 400;
var width = 400;
var means = [];
var clusters = [];
var points_extrema;
var bounds_range;
var delay = 1500;
// data
var points = [
[5, 6],
[23, 17],
[12, 5],
[11, 13],
[4, 12],
[29, 10],
[5, 79],
[15, 57],
[3, 56],
[25, 63],
[8, 45],
[12, 70],
[49, 80],
[42, 85],
[41, 90],
[63, 95],
[60, 83],
[65, 93],
[70, 15],
[75, 25],
[80, 13],
[95, 17],
[83, 8],
[93, 12],
[60, 56],
[65, 42],
[70, 47],
[85, 65],
[90, 50],
[77, 64],
];
// get the bounds for the graph that will be displayed later
function graphBounder(extrema) {
var bounds = [];
for(var i in extrema) {
bounds[i] = extrema[i].max - extrema[i].min;
}
return bounds;
};
// calculate min and max values for graphBounder function
function calcExtrema(inputs) {
var extrema = [];
for(var i in points) {
var input = points[i];
for(var j in input) {
if(!extrema[j]) {
extrema[j] = {min: 9999999, max: -1};
}
if(input[j] < extrema[j].min) {
extrema[j].min = input[j];
}
if(input[j] > extrema[j].max) {
extrema[j].max = input[j];
}
}
}
return extrema;
};
// create random points for means clustering; k is the number of clusters you expect to have
function createRandomPoints(k) {
while(k > 0) {
var mean = [];
for(var i in points_extrema) {
mean[i] = points_extrema[i].min + (Math.random() * bounds_range[i]);
}
means.push(mean);
k -= 1;
}
return means;
};
// assigns each point a cluster
function assignCluster() {
for(var i in points) {
var input = points[i];
var distances = [];
for(var j in means) {
var mean = means[j];
var total = 0;
for(var k in input) {
var diff = input[k] - mean[k];
diff *= diff;
total += diff;
}
distances[j] = Math.sqrt(total);
}
clusters[i] = distances.indexOf(Math.min.apply(null, distances));
}
}
function clusterify() {
assignCluster();
var sums = Array(means.length);
var nums = Array(means.length);
var changed_position = false;
// initialize all values to zero
for(var i in means) {
sums[i] = Array(means[i].length);
for(var j in means[i]) {
sums[i][j] = 0;
}
nums[i] = 0;
}
// fill values
for(var i in clusters) {
var meandex = clusters[i];
var input = points[i];
var mean = means[meandex];
nums[meandex] += 1;
for(var j in mean) {
sums[meandex][j] += input[j];
}
}
for(var i in sums) {
console.log(nums[i]);
if(nums[i] == 0) {
sums[i] = means[i];
console.log("Mean with no points");
console.log(sums[i]);
for(var j in points_extrema) {
sums[i][j] = points_extrema[j].min + (Math.random() * bounds_range[j]);
}
continue;
}
for(var j in sums[i]) {
sums[i][j] /= nums[i];
}
}
if(means.toString() != sums.toString()) {
moved = true;
}
means = sums;
return moved;
}
function display() {
shape.clearRect(0,0,width, height);
// display points
for (var i in points) {
shape.save();
var point = points[i];
var x = (point[0] - points_extrema[0].min + 1) * (width / (bounds_range[0] + 2) );
var y = (point[1] - points_extrema[1].min + 1) * (height / (bounds_range[1] + 2) );
shape.fillStyle = '#2C3E50';
shape.translate(x, y);
shape.beginPath();
shape.arc(0, 0, 5, 0, Math.PI*2, true);
shape.fill();
shape.closePath();
shape.restore();
}
// display means
for (var i in means) {
shape.save();
var point = means[i];
var x = (point[0] - points_extrema[0].min + 1) * (width / (bounds_range[0] + 2) );
var y = (point[1] - points_extrema[1].min + 1) * (height / (bounds_range[1] + 2) );
shape.fillStyle = '#f85c37';
shape.translate(x, y);
shape.beginPath();
shape.arc(0, 0, 5, 0, Math.PI*2, true);
shape.fill();
shape.closePath();
shape.restore();
}
// display lines
shape.globalAlpha = 0.3;
for (var point_index in clusters) {
var mean_index = clusters[point_index];
var point = points[point_index];
var mean = means[mean_index];
shape.save();
shape.strokeStyle = '#f85c37';
shape.beginPath();
shape.moveTo(
(point[0] - points_extrema[0].min + 1) * (width / (bounds_range[0] + 2) ),
(point[1] - points_extrema[1].min + 1) * (height / (bounds_range[1] + 2) )
);
shape.lineTo(
(mean[0] - points_extrema[0].min + 1) * (width / (bounds_range[0] + 2) ),
(mean[1] - points_extrema[1].min + 1) * (height / (bounds_range[1] + 2) )
);
shape.stroke();
shape.closePath();
shape.restore();
}
shape.globalAlpha = 1;
}
function run() {
var something_moved = clusterify();
display();
if(something_moved) {
setTimeout(run, delay);
}
}
function driver() {
canvas = document.getElementById("myCanvas");
shape = canvas.getContext('2d');
points_extrema = calcExtrema(points);
bounds_range = graphBounder(points_extrema);
means = createRandomPoints(5);
assignCluster();
display();
setTimeout(run, delay);
}
driver();