-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkmeans.c
265 lines (232 loc) · 5.47 KB
/
kmeans.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
# include <stdlib.h>
# include <stdio.h>
# include <R_ext/Utils.h>
//allows R_CheckUserInterrupt()
# include "kmeans.h"
/******************************************************************************/
/*
clustr uses the K-means algorithm to cluster data.
Given a matrix of I observations on J variables, the
observations are allocated to N clusters in such a way that the
within-cluster sum of squares is minimised.
Author:
Original FORTRAN77 version by David Sparks.
C version by John Burkardt.
Reference:
David Sparks,
Algorithm AS 58:
Euclidean Cluster Analysis,
Applied Statistics,
Volume 22, Number 1, 1973, pages 126-130.
Parameters:
Input, double X[I*J], the observed data.
Input/output, double D[K*J], the cluster centers.
On input, the user has chosen these. On output, they have been
updated.
Output, double DEV[K], the sums of squared deviations
of observations from their cluster centers.
Output, int B[I], indicates the cluster to which
each observation has been assigned.
Workspace, double F[I].
Output, int E[K], the number of observations assigned
to each cluster.
Input, int I, the number of observations.
Input, int J, the number of variables.
Input, int N, the number of clusters.
Input, int NZ, the minimum number of observations
which any cluster is allowed to have.
Input, int K, the maximum number of clusters. */
void clustr ( double x[], double d[], double dev[], int b[], double f[], int e[], int* i_star, int* j_star, int* n_star, int* nz_star, int* k_star){
int i = *i_star;
int j = *j_star;
int n = *n_star;
int nz = *nz_star;
int k = *k_star;
double big = 1.0E+10;
double da;
double db;
double dc;
double de;
double fl;
double fm;
double fq;
int ia;
int ic;
int id;
int ie;
int ig;
int ih;
int ii;
int ij;
int ik;
int il;
int in;
int ip;
int ir;
int is;
int it;
int iu;
int iw;
int ix;
int iy;
for ( ia = 1; ia <= n; ia++ )
{
e[ia-1] = 0;
}
/*
For each observation, calculate the distance from each cluster
center, and assign to the nearest.
*/
for ( ic = 1; ic <= i; ic++ )
{
f[ic-1] = 0.0;
da = big;
for ( id = 1; id <= n; id++ )
{
db = 0.0;
for ( ie = 1; ie <= j; ie++ )
{
dc = x[ic-1+(ie-1)*i] - d[id-1+(ie-1)*k];
db = db + dc * dc;
}
if ( db < da )
{
da = db;
b[ic-1] = id;
}
}
ig = b[ic-1];
e[ig-1] = e[ig-1] + 1;
}
/*
Calculate the mean and sum of squares for each cluster.
*/
for ( ix = 1; ix <= n; ix++ )
{
dev[ix-1] = 0.0;
for ( iy = 1; iy <= j; iy++ )
{
d[ix-1+(iy-1)*k] = 0.0;
}
}
for ( ic = 1; ic <= i; ic++ )
{
ig = b[ic-1];
for ( ih = 1; ih <= j; ih++ )
{
d[ig-1+(ih-1)*k] = d[ig-1+(ih-1)*k] + x[ic-1+(ih-1)*i];
}
}
for ( ij = 1; ij <= j; ij++ )
{
for ( ii = 1; ii <= n; ii++ )
{
d[ii-1+(ij-1)*k] = d[ii-1+(ij-1)*k] / ( double ) e[ii-1];
}
}
for ( ij = 1; ij <= j; ij++ )
{
for ( ik = 1; ik <= i; ik++ )
{
il = b[ik-1];
da = x[ik-1+(ij-1)*i] - d[il-1+(ij-1)*k];
db = da * da;
f[ik-1] = f[ik-1] + db;
dev[il-1] = dev[il-1] + db;
}
}
for ( ik = 1; ik <= i; ik++ )
{
il = b[ik-1];
fl = e[il-1];
if ( 2.0 <= fl )
{
f[ik-1] = f[ik-1] * fl / ( fl - 1.0 );
}
}
/*
Examine each observation in turn to see if it should be
reassigned to a different cluster.
*/
for ( ; ; )
{
R_CheckUserInterrupt();
iw = 0;
for ( ik = 1; ik <= i; ik++ )
{
il = b[ik-1];
ir = il;
/*
If the number of cluster points is less than or equal to the
specified minimum, NZ, then bypass this iteration.
*/
if ( nz < e[il-1] )
{
fl = e[il-1];
dc = f[ik-1];
for ( in = 1; in <= n; in++ )
{
if ( in != il )
{
fm = e[in-1];
fm = fm / ( fm + 1.0 );
de = 0.0;
for ( ip = 1; ip <= j; ip++ )
{
da = x[ik-1+(ip-1)*i] - d[in-1+(ip-1)*k];
de = de + da * da * fm;
}
if ( de < dc )
{
dc = de;
ir = in;
}
}
}
/*
Reassignment is made here if necessary.
*/
if ( ir != il )
{
fq = e[ir-1];
dev[il-1] = dev[il-1] - f[ik-1];
dev[ir-1] = dev[ir-1] + dc;
e[ir-1] = e[ir-1] + 1;
e[il-1] = e[il-1] - 1;
for ( is = 1; is <= j; is++ )
{
d[il-1+(is-1)*k] = ( d[il-1+(is-1)*k]
* fl - x[ik-1+(is-1)*i] ) / ( fl - 1.0 );
d[ir-1+(is-1)*k] = ( d[ir-1+(is-1)*k]
* fq + x[ik-1+(is-1)*i] ) / ( fq + 1.0 );
}
b[ik-1] = ir;
for ( it = 1; it <= i; it++ )
{
ij = b[it-1];
if ( ij == il || ij == ir )
{
f[it-1] = 0.0;
for ( iu = 1; iu <= j; iu++ )
{
da = x[it-1+(iu-1)*i] - d[ij-1+(iu-1)*k];
f[it-1] = f[it-1] + da * da;
}
fl = e[ij-1];
f[it-1] = f[it-1] * fl / ( fl - 1.0 );
}
}
iw = iw + 1;
}
}
}
/*
If any reassignments were made on this pass, then do another pass.
*/
if ( iw == 0 )
{
break;
}
}
return;
}