-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathKmeans.cs
206 lines (180 loc) · 6.21 KB
/
Kmeans.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AforgenetTest
{
public class Kmeans
{
static int SUCCESS = 1;
static int FAILURE = 0;
static int TRUE = 1;
static int FALSE = 0;
static int MAXVECTDIM = 20;
static int MAXPATTERN = 20;
static int MAXCLUSTER = 10;
class aCluster
{
public double[] Center = new double[MAXVECTDIM];
public int[] Member = new int[Kmeans.MAXPATTERN]; //Index of Vectors belonging to this cluster
public int NumMembers;
}
class aVector
{
public double[] Center = new double[Kmeans.MAXVECTDIM];
public int Size;
}
double[,] Pattern = new double[MAXPATTERN, MAXVECTDIM + 1];
//map<TPoint, RGBQUAD> m_mapPattern;
aCluster[] Cluster = new aCluster[MAXCLUSTER];
int NumPatterns; // Number of patterns
int SizeVector; // Number of dimensions in vector
int NumClusters; // Number of clusters
public int LoadPatterns()
{
int i, j;
double x = 0;
NumPatterns = 10; // Read # of patterns
SizeVector = 1; // Read dimension of vector
NumClusters = 3; // Read # of clusters for K-Means
for (i = 0; i < NumPatterns; i++)
{ // For each vector
for (j = 0; j < SizeVector; j++)
{ // create a pattern
Pattern[i, j] = x;
x += i / 2;
}
}
return SUCCESS;
}
public void InitClusters()
{
int i, j;
for (i = 0; i < NumClusters; i++)
{
Cluster[i].Member[0] = i;
for (j = 0; j < SizeVector; j++)
{
Cluster[i].Center[j] = Pattern[i, j];
}
}
}
public void RunKMeans()
{
int converged;
int pass;
pass = 1;
converged = FALSE;
while (converged == FALSE)
{
DistributeSamples();
converged = CalcNewClustCenters();
ShowClusters();
}
}
private double EucNorm(int p, int c)
{ // Calc Euclidean norm of vector difference
double dist, x; // between pattern vector, p, and cluster
int i; // center, c.
dist = 0;
for (i = 0; i < SizeVector; i++)
{
x = (Cluster[c].Center[i] - Pattern[p, i]) * (Cluster[c].Center[i] - Pattern[p, i]);
dist += x;
}
return dist;
}
private int FindClosestCluster(int pat)
{
int i, ClustID;
double MinDist, d;
MinDist = 9.9e+99;
ClustID = -1;
for (i = 0; i < NumClusters; i++)
{
d = EucNorm(pat, i);
//printf("Distance from pattern %d to cluster %d is %f/n/n",pat,i,sqrt(d));
if (d < MinDist)
{
MinDist = d;
ClustID = i;
}
}
if (ClustID < 0)
{
// printf("Aaargh");
// exit(0);
}
return ClustID;
}
private void DistributeSamples()
{
int i, pat, Clustid, MemberIndex;
//Clear membership list for all current clusters
for (i = 0; i < NumClusters; i++)
{
Cluster[i].NumMembers = 0;
}
for (pat = 0; pat < NumPatterns; pat++)
{
//Find cluster center to which the pattern is closest
Clustid = FindClosestCluster(pat);
//printf("patern %d assigned to cluster %d/n/n",pat,Clustid);
//post this pattern to the cluster
MemberIndex = Cluster[Clustid].NumMembers;
Cluster[Clustid].Member[MemberIndex] = pat;
Cluster[Clustid].NumMembers++;
}
}
private int CalcNewClustCenters()
{
int ConvFlag, VectID, i, j, k;
double[] tmp = new double[MAXVECTDIM];
ConvFlag = TRUE;
for (i = 0; i < NumClusters; i++)
{ //for each cluster
for (j = 0; j < SizeVector; j++)
{ // clear workspace
tmp[j] = 0.0;
}
for (j = 0; j < Cluster[i].NumMembers; j++)
{ //traverse member vectors
VectID = Cluster[i].Member[j];
for (k = 0; k < SizeVector; k++)
{ //traverse elements of vector
tmp[k] += Pattern[VectID, k]; // add (member) pattern elmnt into temp
}
}
for (k = 0; k < SizeVector; k++)
{ //traverse elements of vector
tmp[k] = tmp[k] / Cluster[i].NumMembers;
if (tmp[k] != Cluster[i].Center[k])
ConvFlag = FALSE;
Cluster[i].Center[k] = tmp[k];
}
}
return ConvFlag;
}
public void ShowClusters()
{
int cl, i, pi;
Console.WriteLine();
for (cl = 0; cl < NumClusters; cl++)
{
Console.WriteLine("/n-CLUSTER -%2d--Center: ", cl);
for (pi = 0; pi < SizeVector; pi++)
{
Console.WriteLine(" %2f", Cluster[cl].Center[pi]);
}
Console.WriteLine("----/n ");
for (i = 0; i < Cluster[cl].NumMembers; i++)
{
for (pi = 0; pi < SizeVector; pi++)
{
Console.WriteLine(" %2f ", Pattern[Cluster[cl].Member[i], pi]);
}
}
}
}
}
}