-
Notifications
You must be signed in to change notification settings - Fork 0
/
efcm.h
389 lines (350 loc) · 24.2 KB
/
efcm.h
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/***************************************************************************
* Copyright (C) 2007 by vahid mokhtari and Ramin Fathzadeh *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "load.h"
#define U(i,j) U[j][i]
#define Co_AssocN (((N)*(N))-N)/2
#define Co_Assoc(i,j) Co_Assoc[(((i)*(N))+(j)) - (((i)*(i)+((3*(i))+2))/2)]
/* clustering methods */
#define _FCM "fcm"
#define _EFCM "efcm"
/* consensus methods */
#define RELABELING 0
#define COASSOCIATION 1
/* dataset types */
#define STATIC 0
#define STREAM 1
/* cluster type */
#define JOINED 0 // integrate all data with a cluster label in a one file
#define DISJOINED 1 // put data with the same cluster label in separated files
extern int OS; /* specify the operating system. (defined in main.cpp)
0: windows, 1: linux */
extern int ClusterGeneration;
/* specify how to generate cluster number in each run
of fcm, that can be either 0:random or 1:sequential. */
/***************************************************************************/
/* Fuzzy C-Means Class */
/***************************************************************************/
class FuzzyCMeans
{
public:
/* Constructors */
FuzzyCMeans ( );
FuzzyCMeans ( double Epsilon,
double FuzzyExp,
int NrClusters,
int NrDimensions,
long NrData,
int NrTrueClusters = 2,
int ClusterValidation = 0,
int FirstClusterLabel = 0,
double *MaxValues = NULL );
~ FuzzyCMeans ( );
/* Parameters */
void ResetParameters ( double Epsilon,
double FuzzyExp,
int NrClusters,
int NrDimensions,
long NrData,
double *MaxValues = NULL );
void GetParameters ( double &Epsilon,
double &FuzzyExp,
int &NrClusters,
int &NrDimensions,
long &NrData );
void GetParameters ( char *FileName,
double &Epsilon,
double &FuzzyExp,
int &NrClusters,
int &NrDimensions,
long &NrData );
/* Main functions */
int FCM ( int displayinfo = 1 );
int Init ( );
int UpdateCentroids ( );
double UpdateUmatrix ( );
double ClusterValidation ( );
int GetNrFinalCluster ( );
int GetFinalClusters ( int * f );
int GetClusters ( double ** v );
/* Utilities */
int IsExampleCentroid ( int k );
double Distance ( double * v1, double * v2 );
/* Public Output Utilities */
double ** GetCentroids ( );
double ** GetUmatrix ( );
int * GetMembers ( int * m );
int WriteCentroids ( char * filename );
int WriteUmatrix ( char * filename );
int WriteMembers ( char * filename );
int WriteClusters ( char * filename );
int WriteMRIClusters ( int col = 256,
char * filename = NULL );
/* Referral **X to external **x data */
void SetData ( double ** x );
void SetTrueClusters ( int * tc );
void SetMaxValue ( double * maxvalue );
/* Print info into console. */
void PrintParameters ( int display = 1 );
void PrintData ( int display = 1 );
protected:
double Epsilon; // square error threshold
double m; // fuzzification exponent
int C; // number of clusters
int D; // dimension of input data
long N; // number of input data
int NumberOfIterations;
double ** V; // cluster prototypes (centers) V[C][D]
double ** U; // membership matrix U[N][C]
double ** X; // input data X[N][D]
double * MaxValue; // maximum value of attributes (used in first random init) MaxValue[D]
/* cluster validation attributes */
int K; // number of true clusters (labels) ,used in validity
int S; // first cluster label ,used in validity
int * TC; // true clusters (labels) TC[N] ,used in validity
int Validation; // validate cclustering result
long ** ConfusionMatrix; /* Virtually all criteria for comparing clusterings can be computed given
the so-called confusion matrix. Assume that we have two clusterings
C ={C1,C2, . . . ,CK} and C' = {C'1,C'2 , . . . ,C'K'}. The confusion
matrix M =(mij) is a K x K' matrix whose ijth element is the number
of points in the intersection of clusters Ci and C'j ,
i.e., mij = |Ci \ C'j |. */
};
/***************************************************************************/
/* Ensemble Fuzzy C-Means Class */
/***************************************************************************/
class EnssembleFuzzyCMeans
{
public:
/* Constructors */
EnssembleFuzzyCMeans ( );
EnssembleFuzzyCMeans ( double MinEpsilon = 0.0001,
double MaxEpsilon = 0.0001,
double MinFuzzyexp = 2.0,
double MaxFuzzyexp = 3.0,
int MinCluster = 2,
int MaxCluster = 2,
long NrData = 2,
int NrDimension = 2,
int NrTrueClusters = 2,
int FirstClusterLabel = 0,
int NrClusterings = 2,
int ClusterValidation = 0,
int NrForceClusters = 0,
int ConsensusMethod = COASSOCIATION );
~ EnssembleFuzzyCMeans ( );
/* Parameters */
void ResetParameters ( double MinEpsilon = 0.0001,
double MaxEpsilon = 0.0001,
double MinFuzzyexp = 2.0,
double MaxFuzzyexp = 3.0,
int MinCluster = 2,
int MaxCluster = 2,
long NrData = 2,
int NrDimension = 2,
int NrTrueClusters = 2,
int FirstClusterLabel = 0,
int NrClusterings = 2,
int ClusterValidation = 0,
int NrForceClusters = 0,
int ConsensusMethod = COASSOCIATION );
void GetParameters ( double &min_epsilon,
double &max_epsilon,
double &min_fuzzyexp,
double &max_fuzzyexp,
int &min_k, int &max_k,
int &d, long &n, int &c );
/* Main functions */
int EFCM ( char * logfile = NULL );
int Init ( );
int MakeClusters ( char * logfile = NULL );
int * UpdateCoAssocUmatrix( int c = 0, char * logfile = NULL );
double ClusterValidation ( );
int GetNrFinalCluster ( );
int GetFinalClusters ( int * f );
int GetClusters ( double ** v );
/* Referral **X to external **x data */
void SetData ( double **x );
void SetTrueClusters ( int * tc );
void SetMaxValue ( double * maxvalue );
double * GetMaxValue ( );
/* Print info into console. */
void PrintParameters ( int display = 1 );
int WriteMembers ( char * filename );
int WriteCentroids ( char * filename );
int WriteClusters ( char * filename,
int integration = JOINED );
int WriteMRIClusters ( int col = 256,
char * filename = NULL );
int WritePartitions ( char * filename );
int WriteCoAssocMatrix ( char * filename );
int WriteSLTree ( char * filename );
protected:
/* public attributes */
double Min_Epsilon; // minimum square error threshold
double Max_Epsilon; // maximum square error threshold
double Min_m; // maximum fuzzification exponent
double Max_m; // maximum fuzzification exponent
int Min_K; // maximum number of clusters
int Max_K; // maximum number of clusters
int D; // dimension of input data
long N; // length of input data
int C; // number of clusterings (ensembles)
int FC; // number of final clusters
double ** X; // input data X[N][D]
int * F; // final partition (cluster name of each element) F[N]
double ** V; // cluster prototypes (centers) V[FC][D]
int Consensus; // integration strategy (consensus)
double * MaxValue; // maximum value of attributes MaxValue[D]
FuzzyCMeans * fcm;
/* relabeling & voting attributes */
int ** P; // partitions P[C][N] (used in relabeling & voting matrix)
/* evidence accumulation attributes */
int ** FinalClusters; // FinalClusters[C][N]
int ForceC; // number of final clusters which efcm forced to produce them
int * Co_Assoc; // co-association matrix C[N][N] in fact: (C[(N*N-N)/2])
long ** SL[2]; /* single-linkage tree SL[2][N][C]
first dimension is either '0' (left) or '1' (right) which means '0'
contains left neighbor and '1' contains right neighbor in its cluster.
second dimension is the index of data in **X for left and right neighbor.
third dimension is the level of single-linkage tree.
every SL[][i][j] contains the left and right co-clusters for i'th data
in j'th level of single-linkage tree. */
long ** Clusters; /* store number of clusters and level of each partition.
first dimension contains two values involve
cluster-number ([0]) and level of partition ([1]).
second dimension is the maximum partition number ([C+1]),
actually is the number of ensembles */
long Clustersidx; /* index of Clusters[2][C+1] (used in second dimension) */
/* cluster validation attributes */
int K; // number of true clusters (labels) ,used in validity
int S; // first cluster label ,used in validity
int * TC; // true clusters (labels) TC[N] ,used in validity
int Validation; // validate cclustering result
long ** ConfusionMatrix; /* Virtually all criteria for comparing clusterings can be computed given
the so-called confusion matrix. Assume that we have two clusterings
C ={C1,C2, . . . ,CK} and C' = {C'1,C'2 , . . . ,C'K'}. The confusion
matrix M =(mij) is a K x K' matrix whose ijth element is the number
of points in the intersection of clusters Ci and C'j ,
i.e., mij = |Ci \ C'j |. */
};
/***************************************************************************/
/* Stream Ensemble Fuzzy C-Means Class */
/***************************************************************************/
class StreamEnssembleFuzzyCMeans
{
public:
/* Constructors */
StreamEnssembleFuzzyCMeans ( );
StreamEnssembleFuzzyCMeans ( double MinEpsilon = 0.0001,
double MaxEpsilon = 0.0001,
double MinFuzzyexp = 2.0,
double MaxFuzzyexp = 3.0,
int MinCluster = 2,
int MaxCluster = 2,
long NrData = 2,
int NrDimension = 2,
long BlockSize = 2,
int NrTrueClusters = 2,
int FirstClusterLabel = 0,
int NrClusterings = 2,
int ClusterValidation = 0,
int NrForceClusters = 0,
int ConsensusMethod = COASSOCIATION,
char * BaseClusterMethod = _EFCM );
~ StreamEnssembleFuzzyCMeans ( );
/* Parameters */
void ResetParameters ( double MinEpsilon = 0.0001,
double MaxEpsilon = 0.0001,
double MinFuzzyexp = 2.0,
double MaxFuzzyexp = 3.0,
int MinCluster = 2,
int MaxCluster = 2,
long NrData = 2,
int NrDimension = 2,
long BlockSize = 1000,
int NrTrueClusters = 2,
int FirstClusterLabel = 0,
int NrClusterings = 2,
int ClusterValidation = 0,
int NrForceClusters = 0,
int ConsensusMethod = COASSOCIATION,
char * BaseClusterMethod = _EFCM );
/* Main functions */
int Init ( char * logfile = NULL );
int SEFCM ( long block = 1000,
char * logfile = NULL );
int ReCluster ( char * logfile = NULL );
double ClusterValidation ( );
/* Referral **X to external **x data */
void SetData ( double **x );
double ** GetData ( );
void SetTrueClusters ( int * tc );
int * GetTrueClusters ( );
int * GetTrueClusters ( int * tc );
void SetMaxValue ( double * maxvalue );
double * GetMaxValue ( );
/* Print info into console. */
void PrintParameters ( int display = 1 );
int WriteMembers ( char * filename );
int WriteMRIMembers ( char * filename );
int WriteCentroids ( char * filename );
int WriteClusters ( char * filename );
protected:
/* public attributes */
double Min_Epsilon; // minimum square error threshold
double Max_Epsilon; // maximum square error threshold
double Min_m; // maximum fuzzification exponent
double Max_m; // maximum fuzzification exponent
int Min_K; // maximum number of clusters
int Max_K; // maximum number of clusters
int D; // dimension of input data
long N; // length of input data
long B; // size of blocks should be loaded from input data iteratively
int C; // number of clusterings (ensembles)
int FC; // number of final clusters
double ** X; // input data X[N][D]
int * F; // final partition (cluster name of each element) F[N]
double ** V; // cluster prototypes (centers) V[FC][D]
int Consensus; // integration strategy (consensus)
double * MaxValue; // maximum value of attributes MaxValue[D]
char * BaseMethod; // base clustering method which can be either "efcm" or "fcm"
/* evidence accumulation attributes */
int ForceC; // number of final clusters which efcm forced to produce them
/* stream attributes at every block */
int BIdx; // blocks indexes that proceed to 'Blocks'
int NrBlocks; // block numbers that equales to (N / B)
int * BFC; // number of final clusters at every block FC[NrBlocks]
int ** BF; // final partition at every block (cluster name of each element) F[NrBlocks][B]
double *** BV; // cluster prototypes (centers) at every block V[NrBlocks][BFC][D]
long * BL; // data length for each efcm clustering at every block L[NrBlocks]
int ** BTC; // true clusters (labels) at every block TC[NrBlocks][B]
/* cluster validation attributes */
int K; // number of true clusters (labels) ,used in validity
int S; // first cluster label ,used in validity
int * TC; // true clusters (labels) TC[N] ,used in validity
int Validation; // validate cclustering result
long ** ConfusionMatrix; /* Virtually all criteria for comparing clusterings can be computed given
the so-called confusion matrix. Assume that we have two clusterings
C ={C1,C2, . . . ,CK} and C' = {C'1,C'2 , . . . ,C'K'}. The confusion
matrix M =(mij) is a K x K' matrix whose ijth element is the number
of points in the intersection of clusters Ci and C'j ,
i.e., mij = |Ci ^ C'j|. */
};