135{
137 if (k <= 1)
138 {
139
140
141
142
143
145 memset(clusters, 0,
sizeof(
cluster));
147 }
148 else if (k < size)
149 {
151 memset(clusters, 0, k *
sizeof(
cluster));
152
153 for (size_t j = 0; j < size; j++)
154 {
155 observations[j].
group = rand() % k;
156 }
157 size_t changed = 0;
158 size_t minAcceptedError =
159 size /
160 10000;
161 int t = 0;
162 do
163 {
164
165 for (int i = 0; i < k; i++)
166 {
169 clusters[i].
count = 0;
170 }
171
172 for (size_t j = 0; j < size; j++)
173 {
174 t = observations[j].
group;
175 clusters[t].
x += observations[j].
x;
176 clusters[t].
y += observations[j].
y;
178 }
179 for (int i = 0; i < k; i++)
180 {
181 clusters[i].
x /= clusters[i].
count;
182 clusters[i].
y /= clusters[i].
count;
183 }
184
185 changed = 0;
186 for (size_t j = 0; j < size; j++)
187 {
189 if (t != observations[j].group)
190 {
191 changed++;
192 observations[j].
group = t;
193 }
194 }
195 } while (changed > minAcceptedError);
196
197 }
198 else
199 {
200
201
202
204 memset(clusters, 0, k *
sizeof(
cluster));
205 for (int j = 0; j < size; j++)
206 {
207 clusters[j].
x = observations[j].
x;
208 clusters[j].
y = observations[j].
y;
209 clusters[j].
count = 1;
210 observations[j].
group = j;
211 }
212 }
213 return clusters;
214}
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