-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradeToNominal.java
More file actions
251 lines (219 loc) · 6.78 KB
/
GradeToNominal.java
File metadata and controls
251 lines (219 loc) · 6.78 KB
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
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;
public class GradeToNominal
{
private static ArrayList<String[]> data;
public static void main (String[] args)
{
if(args.length != 1)
{
System.out.println("Invalid arguments. Expected 1 argument: filename (no extension) of .csv file");
System.exit(1);
}
data = new ArrayList<String[]>();
//get the filename from teh commandline
String filename = args[0];
//read the file, store data in ArrayList<String[]>
readData(filename+".arff");
//write two files, with different discretizations
writeData_2(filename, data);
writeData_5(filename, data);
}
private static void readData(String filename)
{
try{
//Read the input file and convert to 2d array
File inFile = new File(filename);
if (inFile.isFile()) {
boolean isData = false;
// create BufferedReader and read data from csv
BufferedReader csvReader = new BufferedReader(new FileReader(filename));
String row;
while ((row = csvReader.readLine()) != null) {
String[] tmp = row.split(",");
if(isData)
{
data.add(tmp);
}
else
{
if (row.toLowerCase().equals("@data"))
{
isData = true;
}
}
}
//finally, close the reader
csvReader.close();
}
}
catch(Exception e)
{
System.out.println(e);
System.exit(1);
}
}
private static void writeData_2(String filename, ArrayList<String[]> data)
{
String head = "@relation " + filename + "_2\n\n"
+ "@attribute school {GP,MS}\n"
+ "@attribute sex {M,F}\n"
+ "@attribute age INTEGER\n"
+ "@attribute address {U,R}\n"
+ "@attribute famsize {LE3,GT3}\n"
+ "@attribute Pstatus {T,A}\n"
+ "@attribute Medu INTEGER\n"
+ "@attribute Fedu INTEGER\n"
+ "@attribute Mjob {teacher,health,services,at_home,other}\n"
+ "@attribute Fjob {teacher,health,services,at_home,other}\n"
+ "@attribute reason {home,reputation,course,other}\n"
+ "@attribute guardian {mother,father,other}\n"
+ "@attribute traveltime INTEGER\n"
+ "@attribute studytime INTEGER\n"
+ "@attribute failures INTEGER\n"
+ "@attribute schoolsup {yes,no}\n"
+ "@attribute famsup {yes,no}\n"
+ "@attribute paid {yes, no}\n"
+ "@attribute activities {yes, no}\n"
+ "@attribute nursery {yes, no}\n"
+ "@attribute higher {yes, no}\n"
+ "@attribute internet {yes, no}\n"
+ "@attribute romantic {yes, no}\n"
+ "@attribute famrel INTEGER\n"
+ "@attribute freetime INTEGER\n"
+ "@attribute goout INTEGER\n"
+ "@attribute Dalc INTEGER\n"
+ "@attribute Walc INTEGER\n"
+ "@attribute health INTEGER\n"
+ "@attribute absences INTEGER\n"
+ "@attribute G3 {fail,pass}\n\n"
+ "@data\n";
BufferedWriter bw = null;
try {
//Specify the file name and path here
File file = new File(filename+"_2.arff");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
bw = new BufferedWriter(fw);
//put the .arff head data into the file
bw.write(head);
//write the data
for (String[] row : data)
{
//so we don't overwrite the original
String[] tmp = Arrays.copyOf(row,row.length);
//convert last index (i.e. G3, the final student grade) from number to nominal
//x<=10 = fail ; 10<x = pass
int val = Integer.parseInt(tmp[tmp.length-1]);
if(val<=10)
tmp[tmp.length-1] = "fail";
else
tmp[tmp.length-1] = "pass";
//write into the file
bw.write(Arrays.toString(tmp).replace(" ", "").replace("[", "").replace("]", "\n"));
}
} catch (Exception e) {
System.out.println(e);
System.exit(1);
}
finally
{
//cleanup
try {
if(bw!=null)
bw.close();
} catch(Exception e) {
System.out.println("Error closing writer.");
}
}
}
private static void writeData_5(String filename, ArrayList<String[]> data)
{
String head = "@relation " + filename + "_5\n\n"
+ "@attribute school {GP,MS}\n"
+ "@attribute sex {M,F}\n"
+ "@attribute age INTEGER\n"
+ "@attribute address {U,R}\n"
+ "@attribute famsize {LE3,GT3}\n"
+ "@attribute Pstatus {T,A}\n"
+ "@attribute Medu INTEGER\n"
+ "@attribute Fedu INTEGER\n"
+ "@attribute Mjob {teacher,health,services,at_home,other}\n"
+ "@attribute Fjob {teacher,health,services,at_home,other}\n"
+ "@attribute reason {home,reputation,course,other}\n"
+ "@attribute guardian {mother,father,other}\n"
+ "@attribute traveltime INTEGER\n"
+ "@attribute studytime INTEGER\n"
+ "@attribute failures INTEGER\n"
+ "@attribute schoolsup {yes,no}\n"
+ "@attribute famsup {yes,no}\n"
+ "@attribute paid {yes, no}\n"
+ "@attribute activities {yes, no}\n"
+ "@attribute nursery {yes, no}\n"
+ "@attribute higher {yes, no}\n"
+ "@attribute internet {yes, no}\n"
+ "@attribute romantic {yes, no}\n"
+ "@attribute famrel INTEGER\n"
+ "@attribute freetime INTEGER\n"
+ "@attribute goout INTEGER\n"
+ "@attribute Dalc INTEGER\n"
+ "@attribute Walc INTEGER\n"
+ "@attribute health INTEGER\n"
+ "@attribute absences INTEGER\n"
+ "@attribute G3 {F,D,C,B,A}\n\n"
+ "@data\n";
BufferedWriter bw = null;
try {
//Specify the file name and path here
File file = new File(filename+"_5.arff");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
bw = new BufferedWriter(fw);
//put the .arff head data into the file
bw.write(head);
//write the data
for (String[] row : data)
{
//so we don't overwrite the original
String[] tmp = Arrays.copyOf(row,row.length);
//convert last index (i.e. G3, the final student grade) from number to nominal
//Grades consistent with Ryerson grading range
int val = Integer.parseInt(tmp[tmp.length-1]);
if(val<10)
tmp[tmp.length-1] = "F";
else if(val<12)
tmp[tmp.length-1] = "D";
else if(val<14)
tmp[tmp.length-1] = "C";
else if(val<16)
tmp[tmp.length-1] = "B";
else if(val<=20)
tmp[tmp.length-1] = "A";
//write into the file
bw.write(Arrays.toString(tmp).replace(" ", "").replace("[", "").replace("]", "\n"));
}
} catch (Exception e) {
System.out.println(e);
System.exit(1);
}
finally
{
//cleanup
try {
if(bw!=null)
bw.close();
} catch(Exception e) {
System.out.println("Error closing writer.");
}
}
}
}