-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans.java
172 lines (137 loc) · 5.59 KB
/
kmeans.java
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
package Package;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class kmeans {
public static void main(String[] args) {
try {
/***
*
*Tawfiq Abu Arrh
*
*/
//file location "Change it" to your location
Scanner input = new Scanner(new File("C:\\Users\\Tawfiq\\Desktop\\test_data.txt")); //Windows
//Scanner input = new Scanner(new File("/home/tawfiq/Desktop/test_data.txt")); //Linux
Scanner in = new Scanner(System.in);
String answer;
boolean Exit = true;
int row = 0,Answer = 0;
//Select Mean OR Median
System.out.println("Do you want? \n1.Mean\n2.Median");
while(Exit)
{
Answer = in.nextInt();
if(Answer == 1 ||Answer == 2)
Exit = false;
else
System.out.println("Bad Input,Please Eneter (1,2)");
}
/*
*
*This while loop just for segmant the file and put it in two dimensional
*array named "point[][]"
*
*/
while (input.hasNextLine()) {
int countChar = 0;
String line = input.nextLine();
char[] myChar = line.toCharArray();
for (int i=0;i<myChar.length;i++){
if (myChar[countChar] == ',')
{
insertToX(myChar,0,countChar,row);
insertToY(myChar,(countChar+1),(myChar.length-2),row);
i = 100;//to go the next line
}
else
countChar++;
}
row++;
}
/*
*After finsh it's job
*Print the two dimensional array after we fill it with data
*/
for (double[] arr : point)
System.out.println(Arrays.toString(arr));
//ArrayList to save the centroid to print it after program is finshed
ArrayList<ClusterCentroid> centroids = new ArrayList();
Point pointClass = new Point();
pointClass.getCentroids();
pointClass.cluster();
boolean continue1 = true;
while(continue1){
//Added the Cluster centroid in arraylist above
centroids.add(pointClass.getCentroids(0));
centroids.add(pointClass.getCentroids(1));
centroids.add(pointClass.getCentroids(2));
centroids.add(pointClass.getCentroids(3));
//Ptint the point after this iteration after we cluster it
for (double[] arr : point)
System.out.println(Arrays.toString(arr));
System.out.println("Do you want anthor itteration ? (Y/N)");
answer = in.next();
//If the answer is No Print Centroid that we store in arraylist above
if (answer.equalsIgnoreCase("N"))
{
System.out.println("*******************************************");
continue1 = false;//to exit from the while loop
int k =0;
for (ClusterCentroid object: centroids) {//retrieve data from arraylist above to print it
k++;
System.out.println("(" + object.X() + " , " + object.Y() + ")");
if (k == 4)
{
System.out.println("*******************************************");
k = 0;
}
}
//Calculate The SSE
pointClass.SSE();
}
/*
*If the answer is Yes ,it will make anthor iteration and calculate new centroid
*with Mean OR Median method
*/
else
{
Point SecondPoints = new Point();
//If you select Mean
if(Answer == 1)
{
MeanCentroid mean = new MeanCentroid();
mean.newMeanCentroidData();
}
//If you select Median
else
{
MedianCentroid median = new MedianCentroid();
median.newMedianCentroidData();
}
SecondPoints.printNewCentroid();
SecondPoints.SecondCentroid();
SecondPoints.cluster();
}
}
}catch(Exception ex) {System.err.println("Error in loading file");}
}
public static double[][] point = new double[863][3];
public static void insertToX(char[] myChar, int i, int countChar,int row)
{
char[] k = new char[countChar];
for(i=i;i<countChar;i++)
k[i] = myChar[i];
point[row][0] = Double.parseDouble((String.valueOf(k)));//insert X value in 1st column
}
public static void insertToY(char[] myChar, int i, int countChar,int row)
{
char[] k = new char[countChar];
for(i=i;i<countChar;i++){
k[i] = myChar[i];
}
point[row][1] = Double.parseDouble((String.valueOf(k)));//insert Y value in 2nd column
point [row][2] = Double.parseDouble(String.valueOf(myChar[(myChar.length)-1]));//Insert Cluster value into the 3rd column
}
}