-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkmeans_seq.cpp
60 lines (46 loc) · 1.78 KB
/
kmeans_seq.cpp
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
#include "kmeans.h"
#include <stdlib.h>
#include <float.h>
void kmeans(int iteration_n, int class_n, int data_n, Point* centroids, Point* data, int* partitioned)
{
// Loop indices for iteration, data and class
int i, data_i, class_i;
// Count number of data in each class
int* count = (int*)malloc(sizeof(int) * class_n);
// Temporal point value to calculate distance
Point t;
// Iterate through number of interations
for (i = 0; i < iteration_n; i++) {
// Assignment step
for (data_i = 0; data_i < data_n; data_i++) {
float min_dist = DBL_MAX;
for (class_i = 0; class_i < class_n; class_i++) {
t.x = data[data_i].x - centroids[class_i].x;
t.y = data[data_i].y - centroids[class_i].y;
float dist = t.x * t.x + t.y * t.y;
if (dist < min_dist) {
partitioned[data_i] = class_i;
min_dist = dist;
}
}
}
// Update step
// Clear sum buffer and class count
for (class_i = 0; class_i < class_n; class_i++) {
centroids[class_i].x = 0.0;
centroids[class_i].y = 0.0;
count[class_i] = 0;
}
// Sum up and count data for each class
for (data_i = 0; data_i < data_n; data_i++) {
centroids[partitioned[data_i]].x += data[data_i].x;
centroids[partitioned[data_i]].y += data[data_i].y;
count[partitioned[data_i]]++;
}
// Divide the sum with number of class for mean point
for (class_i = 0; class_i < class_n; class_i++) {
centroids[class_i].x /= count[class_i];
centroids[class_i].y /= count[class_i];
}
}
}