-
Notifications
You must be signed in to change notification settings - Fork 0
/
lvq.c
271 lines (203 loc) · 5.97 KB
/
lvq.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#define inputs 2
#define num_of_means 5
#define dataset_length 600
#define EPOCH_MAX 1000
#define h0 0.01
float data[dataset_length][2];
float weights[num_of_means][2];
int clusters[num_of_means][600];
float h = h0;
float distance(float x1, float y1, float x2, float y2)
{
return sqrt(pow((x2 - x1), 2.0) + pow((y2 - y1), 2.0));
}
void to_file(float centers[][2])
{
FILE *fp1;
fp1 = fopen("centers.txt", "w");
for (int i = 0; i < num_of_means; i ++) {
fprintf(fp1, "%lf,%lf\n", centers[i][0], centers[i][1]);
}
fclose(fp1);
}
int winner_neuron(float center[][2], float data[][2], int n)
{
float d;
int winner;
float min = 1000.0;
for (int i = 0; i < num_of_means; i ++) {
d = distance(center[i][0], center[i][1], data[n][0], data[n][1]);
if (d < min) {
min = d;
winner = i;
}
}
return winner;
}
void init_weights(float stream[][2], int n)
{
int i; // index for elements in stream
for (i = 0; i < num_of_means; i++) {
weights[i][0] = stream[i][0];
weights[i][1] = stream[i][1];
}
srand(time(0));
for (; i < n; i++) {
int j = rand() % (i+1);
//printf("j: %d\n", j);
if (j < num_of_means) {
weights[j][0] = stream[i][0];
weights[j][1] = stream[i][1];
}
}
//printf("Following are k randomly selected items \n");
//for (int i = 0; i < num_of_means; i++)
// printf("%lf, %lf\n", weights[i][0], weights[i][1]);
}
void update_weights(int winner, float x[], int k) {
for (int j = 0; j < k; j ++) {
weights[winner][j] += h * (x[j] - weights[winner][j]);
}
}
void load_data()
{
FILE *fp;
double x1, x2;
fp = fopen("clusters.txt", "r");
for (int i = 0; i < dataset_length; i++) {
fscanf(fp, "%lf,%lf", &x1, &x2);
data[i][0] = (float)x1;
data[i][1] = (float)x2;
//printf("%lf, %lf\n", data[i][0], data[i][1]);
}
fclose(fp);
}
int change(float m1[][2], float m0[][2])
{
float d;
int count = 0;
for (int i = 0; i <num_of_means; i ++) {
d = distance(m1[i][0], m1[i][1], m0[i][0], m0[i][1]);
//printf("dist: %lf\n", d);
if (d <= 0.001) {
count ++;
}
}
if ( count == num_of_means ) {
return 0;
}
return 1;
}
float error(int clusters[][600], float data[][2], float center[][2])
{
int index;
float sum1 = 0;
float sum2;
for (int i = 0; i < num_of_means; i ++) {
int len = clusters[i][0];
sum1 = 0;
for (int j = 1; j <= len; j ++) {
index = clusters[i][j];
sum1 += distance(center[i][0], center[i][1], data[index][0], data[index][1]);
}
sum2 += sum1;
}
return sum2;
}
void classify(float data[][2], float center[][2])
{
int temp_cluster;
int next;
float dist;
float min = 1000.0;
for (int i = 0; i < dataset_length; i++) {
min = 1000.0;
for (int j = 0; j < num_of_means; j++) {
dist = distance(center[j][0], center[j][1], data[i][0], data[i][1]);
if (dist < min) {
min = dist;
temp_cluster = j;
}
}
//cluster[i] = temp_cluster;
next = clusters[temp_cluster][0] + 1;
clusters[temp_cluster][next] = i;
clusters[temp_cluster][0] ++;
}
}
int main()
{
int winner;
float min_error = 1000.0;
float err;
float weights0[num_of_means][2];
float min_centers[num_of_means][2];
int min_k;
load_data();
for (int k = 0; k < 5; k ++) {
printf("k = %d\n", k);
init_weights(data, dataset_length);
printf("centers: ");
for (int i = 0; i < num_of_means; i ++) {
printf("*%lf,%lf ", min_centers[i][0], min_centers[i][1]);
}
printf("\n");
h = h0;
for (int i = 0; i < num_of_means; i++) {
weights0[i][0] = weights[i][0];
weights0[i][1] = weights[i][1];
//printf("")
}
for (int epoch = 0; epoch < EPOCH_MAX; epoch ++) {
for (int i = 0; i < num_of_means; i ++) {
clusters[i][0] = 0;
}
//printf("epoch: %d\t ", epoch);
for (int n = 0; n < dataset_length; n ++) {
winner = winner_neuron(weights, data, n);
update_weights(winner, data[n], inputs);
}
classify(data, weights);
if (change(weights, weights0) == 0) {
goto exit;
}
err = error(clusters, data, weights);
printf("epoch: %d error: %lf centers: ", epoch, err);
printf("centers: ");
for (int i = 0; i < num_of_means; i ++) {
printf("%lf,%lf ", weights[i][0], weights[i][1]);
}
printf("\n");
for( int i = 0; i < num_of_means; i++) {
weights0[i][0] = weights[i][0];
weights0[i][1] = weights[i][1];
}
h = 0.95 * h;
}
exit:
//printf("exited: \n");
if (err < min_error) {
min_error = err;
for (int i = 0; i < num_of_means; i ++) {
min_centers[i][0] = weights[i][0];
min_centers[i][1] = weights[i][1];
min_k = k;
}
}
}
printf("\n");
printf("k: %d error: %lf centers: ", min_k, min_error);
printf("centers: ");
for (int i = 0; i < num_of_means; i ++) {
printf("%lf,%lf ", min_centers[i][0], min_centers[i][1]);
}
printf("\n");
to_file(min_centers);
execl("/usr/bin/python3", "python3", "plot.py", "clusters.txt", "centers.txt", NULL);
}