145 memset(clusters, 0,
sizeof(
cluster));
151 memset(clusters, 0, k *
sizeof(
cluster));
153 for (
size_t j = 0; j < size; j++)
155 observations[j].
group = rand() % k;
158 size_t minAcceptedError =
165 for (
int i = 0; i < k; i++)
169 clusters[i].
count = 0;
172 for (
size_t j = 0; j < size; j++)
174 t = observations[j].
group;
175 clusters[t].
x += observations[j].
x;
176 clusters[t].
y += observations[j].
y;
179 for (
int i = 0; i < k; i++)
181 clusters[i].
x /= clusters[i].
count;
182 clusters[i].
y /= clusters[i].
count;
186 for (
size_t j = 0; j < size; j++)
189 if (t != observations[j].group)
192 observations[j].
group = t;
195 }
while (changed > minAcceptedError);
204 memset(clusters, 0, k *
sizeof(
cluster));
205 for (
int j = 0; j < size; j++)
207 clusters[j].
x = observations[j].
x;
208 clusters[j].
y = observations[j].
y;
209 clusters[j].
count = 1;
210 observations[j].
group = j;
int calculateNearst(observation *o, cluster clusters[], int k)
Definition: k_means_clustering.c:69
void calculateCentroid(observation observations[], size_t size, cluster *centroid)
Definition: k_means_clustering.c:97
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18
Definition: k_means_clustering.c:53