Skip to content

Commit f30b01a

Browse files
Update k-means.java
Updated Check Equality method
1 parent 20fd184 commit f30b01a

File tree

1 file changed

+78
-20
lines changed

1 file changed

+78
-20
lines changed

src/k-means.java

+78-20
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,22 @@ public static void main(String[]args) throws IOException
5353

5454
ArrayList<double[]> cluster = new ArrayList<double[]>();
5555
ArrayList<ArrayList<double[]>> masterclusters = new ArrayList<ArrayList<double[]>>();
56-
ArrayList<ArrayList<double[]>> oldcluster = new ArrayList<ArrayList<double[]>>();
56+
//Old ArrayList<ArrayList<double[]>> oldcluster = new ArrayList<ArrayList<double[]>>();
5757

5858
for (int a=0; a<NumOfClstr; a++) {
5959

6060
cluster= new ArrayList<double[]>(cluster);
6161
masterclusters.add(cluster);
62-
62+
63+
/**** Do not use unless you want to use the old check equality method ****
6364
cluster= new ArrayList<double[]>(cluster);
6465
oldcluster.add(cluster);
65-
66+
*************************************************************************/
6667
}
6768

6869
ArrayList<double[]> centroid=new ArrayList<double[]>();
70+
ArrayList<double[]> oldcentroids = new ArrayList<double[]>();
71+
6972
System.out.println("\nPlease pick a number between 1 and "+dataset.length+"\n\n");
7073

7174
for(int b=1; b<NumOfClstr+1; b++) {
@@ -76,7 +79,8 @@ public static void main(String[]args) throws IOException
7679
int input = sc.nextInt();
7780

7881
centroid.add(dataset[input-1]);
79-
82+
oldcentroids.add(dataset[input - 1]);
83+
8084
if(b==NumOfClstr) {
8185

8286
sc.close();
@@ -118,10 +122,13 @@ public static void main(String[]args) throws IOException
118122

119123
}
120124

121-
oldcluster=resetcluster(oldcluster,masterclusters);
125+
// Old oldcluster=resetcluster(oldcluster,masterclusters);
126+
oldcentroids = resetcluster(oldcentroids, centroid);
127+
122128
int iterations=0;
123129
long startTime = System.nanoTime();
124-
130+
int rand = 0;
131+
125132
while(true) {
126133

127134
for (int g=0; g<masterclusters.size();g++) {
@@ -161,12 +168,29 @@ public static void main(String[]args) throws IOException
161168

162169
print2D(ConvToArr(centroid));
163170
iterations++;
164-
171+
/* Old
165172
if(iterations >= MaxIt || checkEquality(oldcluster, masterclusters))
166173
break;
167174
else
168175
oldcluster=resetcluster(oldcluster, masterclusters);
169-
176+
*/
177+
if (iterations >= MaxIt)
178+
break;
179+
180+
else if (checkEquality(centroid, oldcentroids)) {
181+
182+
rand++;
183+
if (rand == 10)
184+
break;
185+
186+
}
187+
else {
188+
rand=0;
189+
}
190+
191+
oldcentroids = resetcluster(oldcentroids, centroid);
192+
193+
170194
}
171195

172196
long endTime = System.nanoTime();
@@ -267,40 +291,63 @@ public static double[][] ConvToArr(ArrayList<double[]> input)
267291
}
268292

269293
//Checks if old clusters are equal to new clusters
270-
static boolean checkEquality(ArrayList<ArrayList<double[]>> oldClusters, ArrayList<ArrayList<double[]>> newClusters)
294+
295+
/*****Use this only when you want to compare each member of the cluster to the old cluster. [Comparatively slower Process]*****
296+
297+
static boolean checkEqual(ArrayList<ArrayList<double[]>> oldClusters, ArrayList<ArrayList<double[]>> newClusters)
271298
{
272-
for(int i=0; i<newClusters.size(); i++) {
299+
for (int i = 0; i < newClusters.size(); i++) {
273300
274-
if(oldClusters.size() != newClusters.size())
301+
for (int j = 0; j < newClusters.get(i).size(); j++) {
275302
276-
return false;
303+
if (oldClusters.get(i).size() != newClusters.get(i).size()) {
304+
return false;
277305
278-
for(int j=0; j<newClusters.get(i).size(); j++) {
306+
}
307+
out: while (true) {
308+
for (double[] check1 : oldClusters.get(i)) {
279309
280-
if(oldClusters.get(i).size() != newClusters.get(i).size()) {
310+
System.out.println(check1 + " " + newClusters.get(i).get(j));
281311
282-
return false;
312+
if (check1 == newClusters.get(i).get(j)) {
313+
314+
break out;
315+
316+
}
283317
318+
}
319+
return false;
284320
}
285-
for(int v=0;v<newClusters.get(i).get(j).length;v++ ) {
321+
}
322+
323+
}
286324
287-
if(oldClusters.get(i).get(j)[v] != newClusters.get(i).get(j)[v]) {
325+
return true;
288326
289-
return false;
327+
}
328+
********************************************************************************************************/
290329

291-
}
330+
//checks if old centroid is equal to new centroid
331+
static boolean checkEquality(ArrayList<double[]> oldCentroids, ArrayList<double[]> newCentroids) {
332+
for (int i = 0; i < newCentroids.size(); i++) {
333+
334+
for (int j = 0; j < newCentroids.get(i).length; j++) {
335+
336+
if (Math.abs(oldCentroids.get(i)[j] - newCentroids.get(i)[j]) > 0.000001) {
337+
338+
return false;
292339

293340
}
294341

295342
}
296-
297343
}
298344

299345
return true;
300346

301347
}
302348

303349
//sets old clusters equal to new clusters
350+
/*********Do not use unless you want to use old check equality method****************
304351
static ArrayList<ArrayList<double[]>> resetcluster(ArrayList<ArrayList<double[]>> oldClusters, ArrayList<ArrayList<double[]>> newClusters)
305352
{
306353
for(int i=0; i<oldClusters.size(); i++) {
@@ -324,7 +371,18 @@ static ArrayList<ArrayList<double[]>> resetcluster(ArrayList<ArrayList<double[]>
324371
return oldClusters;
325372
326373
}
374+
********************************************************************************************************/
375+
// updated reset cluster for comparing centroids
376+
377+
static ArrayList<double[]> resetcluster(ArrayList<double[]> oldCentroids, ArrayList<double[]> newCentroids) {
378+
for (int i = 0; i < oldCentroids.size(); i++) {
327379

380+
oldCentroids.set(i, newCentroids.get(i));
381+
382+
}
383+
return oldCentroids;
384+
}
385+
328386
public static void print2D(double mat[][])
329387
{
330388
// Loop through all rows

0 commit comments

Comments
 (0)