-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKFCV.java
336 lines (252 loc) · 9.75 KB
/
KFCV.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
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
package cross_validation;
import java.io.*;
import java.util.*;
public class KFCV {
public static void main(String[] args) throws IOException {
//reading the CSV file
List<String[]> rowList = new ArrayList<String[]>();
int k = Integer.parseInt(args[1]);
//try (BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Rajiv R\\Desktop\\Assignments\\fourth sem- fall\\machine learning_jesus\\assignment_1\\ecoli.csv"))) {
try (BufferedReader br = new BufferedReader(new FileReader(args[0]))) {
String line;
while ((line = br.readLine()) != null) {
String[] lineItems = line.split(",");
rowList.add(lineItems);
}
br.close();
}
catch(Exception e){
// Handle any I/O problems
}
//converting CSV file to a 2D matrix
String[][] matrix = new String[rowList.size()][];
for (int i = 0; i < rowList.size(); i++) {
String[] row = rowList.get(i);
matrix[i] = row;
}
/*System.out.println(matrix.length); // number of rows
System.out.println(matrix[0].length); // number of columns */
int totalrows = matrix.length;
int totalcols = matrix[0].length;
/*System.out.println("array is");
Print2dstring(matrix); // printing the matrix */
// dividing the array into attribute values array,
//class array, attribute name array
//step1 : getting attribute names array
String[] att_names = new String[totalcols];
for(int j = 0; j < totalcols; j++){
att_names[j] = matrix[0][j];
}
//Print1dstring(att_names);
//step2 : getting class array
String[] class_names = new String[totalrows-1];
for(int z = 0; z < totalrows-1; z++){
class_names[z] = matrix[z+1][totalcols-1];
}
//Print1dstring(class_names);
//step3 : getting attribute values array
Double[][] att_values = new Double[totalrows-1][totalcols-1];
for(int i = 0; i < totalrows-1; i++)
{
for(int j = 0; j < totalcols-1; j++)
att_values[i][j]= Double.parseDouble(matrix[i+1][j]) ;
}
// Print2ddouble(att_values);
// normalize the attribute values
Double[][] att_values_normalized = new Double[totalrows-1][totalcols-1];
att_values_normalized = normalize(att_values);
//Print2ddouble(att_values_normalized);
// partitioning the data
Double[][] att_values_partioned = new Double[totalrows-1][totalcols];
att_values_partioned = partition(att_values_normalized,k);
//Print2ddouble(att_values_partioned);
//Combining all the values into a single array
int final_rows = matrix.length;
int final_cols = matrix[0].length+1;
String[][] Final_norm_partitioned = new String[final_rows][final_cols];
Final_norm_partitioned = combine(Final_norm_partitioned,att_values_partioned,att_names,class_names,att_values_normalized);
Print2dstring(Final_norm_partitioned);
/* writing the final result to a CSV file
* The final CSV file is named output.csv */
StringBuilder builder = new StringBuilder();
for(int i = 0; i < Final_norm_partitioned.length; i++)//for each row
{
for(int j = 0; j < Final_norm_partitioned[0].length; j++)//for each column
{
builder.append(Final_norm_partitioned[i][j]+"");//append to the output string
if(j < Final_norm_partitioned.length - 1)//if this is not the last row element
builder.append(",");//then add comma (if you don't like commas you can use spaces)
}
builder.append("\n");//append new line at the end of the row
}
BufferedWriter writer = new BufferedWriter(new FileWriter("output.csv"));
writer.write(builder.toString());//save the string representation of the board
writer.close();
} // end of main
/* this function creates a final array by combining
* all the arrays */
private static String[][] combine(String[][] final_norm_partitioned, Double[][] att_values_partioned,
String[] att_names, String[] class_names, Double[][] att_values_normalized) {
// first : inserting the attribute names
for(int j = 0; j < final_norm_partitioned[0].length-1; j++){
final_norm_partitioned[0][j]=att_names[j] ;
}
final_norm_partitioned[0][final_norm_partitioned[0].length-1] = "Partion_number";
// second : inserting class names into the array
for(int c = 0; c < class_names.length; c++){
final_norm_partitioned[c+1][final_norm_partitioned[0].length-2]=class_names[c] ;
}
// third : adding the normalized values
for(int a=0;a<att_values_normalized.length;a++){
for(int b=0;b<att_values_normalized[0].length;b++){
final_norm_partitioned[a+1][b]= String.valueOf(att_values_normalized[a][b]);
}
}
// last: Assigning the partition number
for(int a=0;a<att_values_partioned.length;a++){
final_norm_partitioned[a+1][final_norm_partitioned[0].length-1]=String.valueOf(att_values_partioned[a][att_values_partioned[0].length-1]);
}
//Print2dstring(final_norm_partitioned);
return final_norm_partitioned;
} // end of combine()
// this fuction partitions the normalized data
private static Double[][] partition(Double[][] normalized,int k) {
// getting number of rows and cols of normalized array
int norm_rows = normalized.length;
int norm_cols = normalized[0].length;
//adding an extra column to store partition number
Double[][] partioned = new Double[norm_rows][norm_cols+ 1];
//System.out.println(partioned.length+" "+partioned[0].length);
int last_column = partioned[0].length;
//System.out.println(last_column);
//copy the norm array to partition array
for(int a=0;a<norm_rows;a++){
for(int b=0;b<norm_cols;b++){
partioned[a][b] = normalized[a][b];
}
} //end of copy
//initializing last cloumn to 0
for(int f=0;f<partioned.length;f++){
partioned[f][last_column-1] = 0.0;
}
//Print2ddouble(partioned);
// assigning partition numbers
int j=0, Low =0,Rand_row =0, low1=1, high =norm_rows-1;
Double Rand_part_num =0.0;
Random r = new Random();
int div = (norm_rows/k);
for(int i=0;i<k;i++){
while(j<div){
Rand_row = r.nextInt(high-Low) + Low;
if(partioned[Rand_row][last_column-1]== 0.0){
partioned[Rand_row][last_column-1] = (double) i + 1.0;
j= j+1;
} else continue;
} //end while
j=0;
//Print2ddouble(partioned);
}//end for
//Print2ddouble(partioned);
// counting the number of excess rows in the partition
int count =0;
for(int f=0;f<partioned.length;f++){
if(partioned[f][last_column-1] == 0.0)
count++;
}
//System.out.println("count1="+count);
/* now my logic for excess partition
* if the number if excess rows is greater than
* or equal to k/2 keep the new partition
* else assign it to existing partition randomly */
if(count >=(k/2)){
for(int f=0;f<partioned.length;f++){
if(partioned[f][last_column-1] == 0.0)
{partioned[f][last_column-1] = k +1.0;}
}
} else {
Random r1 = new Random();
int low2=1;
int Rand_partition = r.nextInt(k-low2) + low2;
for(int f=0;f<partioned.length;f++){
if(partioned[f][last_column-1] == 0.0)
{partioned[f][last_column-1] = Rand_partition +1.0;}
}
}
return partioned;
//testing
/*int count2 =0;
for(int f=0;f<partioned.length;f++){
if(partioned[f][last_column-1] == 4.0)
count2++;
}
System.out.println("count2="+count2);*/
//Print2ddouble(partioned);
} //end of partition()
// this function normalizes the attribute values
private static Double[][] normalize(Double[][] att_values) {
int attr_totalrows = att_values.length;
int attr_totalcols = att_values[0].length;
Double[] temp = new Double[attr_totalrows];
Double min , max;
//finding the max and min value in each coloumn
for (int i = 0; i < attr_totalcols; i++){
for(int j = 0; j < attr_totalrows; j++){
temp [j] = att_values[j][i];
}
min = getMinValue(temp);
max = getMaxValue(temp);
//normalizing
for(int v = 0; v < attr_totalrows; v++){
att_values[v][i] = (att_values[v][i] - min) / (max - min);
}
}
//Print1ddouble(temp);
return att_values;
} //end of normalize
private static Double getMinValue(Double[] temp2) {
Arrays.sort(temp2);
Double min = temp2[0];
return min;
} // end of getMinValue()
//get the max of the temp array
private static Double getMaxValue(Double[] temp1) {
Arrays.sort(temp1);
Double max = temp1[temp1.length-1];
return max;
} //end of getMaxValue()
// this function prints 1d string array
private static void Print1dstring(String[] att_names) {
for(int i = 0; i < att_names.length; i++) {
System.out.println(att_names[i]);
}
} //end of Print1dstring
private static void Print1ddouble(Double[] att_names) {
for(int i = 0; i < att_names.length; i++) {
System.out.println(att_names[i]);
}
} // end of Print1ddouble()
// this function prints 2d string array
private static void Print2dstring(String[][] matrix) {
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[i].length; j++)
{
System.out.print(matrix[i][j]);
if(j < matrix[i].length - 1) System.out.print(" ");
}
System.out.println();
}
} //end of Print2dstring
// this function prints 2d string array
private static void Print2ddouble(Double[][] tempvalues) {
for(int i = 0; i < tempvalues.length; i++)
{
for(int j = 0; j < tempvalues[i].length; j++)
{
System.out.print(tempvalues[i][j]);
if(j < tempvalues[i].length - 1) System.out.print(" ");
}
System.out.println();
}
} //end of Print2dstring
}// end of whole program