-
Notifications
You must be signed in to change notification settings - Fork 12
/
example1.c
102 lines (84 loc) · 1.99 KB
/
example1.c
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
#include <math.h>
#include <stdio.h>
#include "kmeans.h"
static double d_distance(const Pointer a, const Pointer b)
{
double da = *((double*)a);
double db = *((double*)b);
return fabs(da - db);
}
static void d_centroid(const Pointer * objs, const int * clusters, size_t num_objs, int cluster, Pointer centroid)
{
int i;
int num_cluster = 0;
double sum = 0;
double **doubles = (double**)objs;
double *dcentroid = (double*)centroid;
if (num_objs <= 0) return;
for (i = 0; i < num_objs; i++)
{
/* Only process objects of interest */
if (clusters[i] != cluster)
continue;
sum += *(doubles[i]);
num_cluster++;
}
if (num_cluster)
{
sum /= num_cluster;
*dcentroid = sum;
}
return;
}
int
main(int nargs, char **args)
{
double v[10] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
double c[2] = {2.0, 5.0};
kmeans_config config;
kmeans_result result;
int i;
config.num_objs = 11;
config.k = 2;
config.max_iterations = 100;
config.distance_method = d_distance;
config.centroid_method = d_centroid;
config.objs = calloc(config.num_objs, sizeof(Pointer));
config.centers = calloc(config.k, sizeof(Pointer));
config.clusters = calloc(config.num_objs, sizeof(int));
/* populate objs */
for (i = 0; i < config.num_objs - 1; i++)
{
config.objs[i] = &(v[i]);
}
config.objs[10] = NULL;
// config.objs = objs;
/* populate centroids */
for (i = 0; i < config.k; i++)
{
config.centers[i] = &(c[i]);
}
/* run k-means */
result = kmeans(&config);
/* print result */
for (i = 0; i < config.num_objs; i++)
{
if (config.objs[i])
printf("%g [%d]\n", *((double*)config.objs[i]), config.clusters[i]);
else
printf("NN [%d]\n", config.clusters[i]);
}
free(config.objs);
free(config.clusters);
free(config.centers);
return 0;
}
// typedef struct kmeans_config
// {
// kmeans_distance_method object_distance;
// kmeans_centroid_method object_centroid;
// size_t num_objs;
// const object *objs;
// unsigned int k;
// unsigned int max_iterations;
// } kmeans_config;