-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathDataSet.java
More file actions
317 lines (272 loc) · 8.23 KB
/
DataSet.java
File metadata and controls
317 lines (272 loc) · 8.23 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
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
import java.util.*;
import java.io.*;
/**
* This class represents a dataset, including names for the classes,
* all attributes and their values. The class also includes a
* constructor that can read the dataset from data files, as well as a
* method for printing the predictions of a classifier on each of the
* test examples in the format required for submission.
**/
public class DataSet {
/** number of training examples **/
public int numTrainExs;
/** number of test examples **/
public int numTestExs;
/** an array of training examples, each of which is itself an
* array of integer values so that <tt>trainEx[i][a]</tt> is the
* value of attribute <tt>a</tt> on example <tt>i</tt> **/
public int trainEx[][];
/** an array of labels for the training examples **/
public int trainLabel[];
/** an array of test examples, each one an array of integer values **/
public int testEx[][];
/** number of attributes **/
public int numAttrs;
/** the names of the attributes **/
public String attrName[];
/** an array of names for the attribute values:
* for discrete attribute <tt>a</tt>, <tt>attrVals[a][j]</tt> is
* the name of the <tt>j</tt>-th value; if <tt>a</tt> is a numeric
* attribute then <tt>attrVals[a]</tt> is <tt>null</tt> **/
public String attrVals[][];
/** names of the two classes **/
public String className[] = null;
/** This constructor constructs an empty dataset with no training
* examples, no test examples, no attributes, and two classes with
* default names.
*/
public DataSet() {
numTrainExs = 0;
numTestExs = 0;
trainEx = new int[0][];
trainLabel = new int[0];
testEx = new int[0][];
numAttrs = 0;
attrName = new String[0];
attrVals = new String[0][];
className = new String[2];
className[0] = "0";
className[1] = "1";
}
/** This constructor reads in data from the files
* <tt>filestem.names</tt>, <tt>filestem.train</tt> and
* <tt>filestem.test</tt>, and then sets up all of the public
* fields. See assignment instructions for information on the
* required format of these files.
**/
public DataSet(String filestem)
throws FileNotFoundException, IOException {
String[] words = null;
// read .names file
open_file(filestem + ".names");
ArrayList<String[]> attr_list = new ArrayList<String[]>();
String line;
while((line = read_line()) != null) {
line = line.trim( );
words = line.split("\\s+");
if (line.equals(""))
continue;
if (className == null) {
if (words.length != 2) {
String err = "expected two class names at line "
+ line_count + " in file " + filename;
System.err.println(err);
throw new RuntimeException(err);
}
className = words;
} else {
if (words.length <= 1) {
String err = "expected attribute description at line "
+ line_count + " in file " + filename;
System.err.println(err);
throw new RuntimeException(err);
}
attr_list.add(words);
numAttrs++;
}
}
in.close();
attrName = new String[numAttrs];
attrVals = new String[numAttrs][];
for (int i = 0; i < numAttrs; i++) {
words = attr_list.get(i);
attrName[i] = words[0];
if (words[1].equals("numeric")) {
attrVals[i] = null;
} else {
attrVals[i] = new String[words.length - 1];
for (int j = 1; j < words.length; j++) {
attrVals[i][j-1] = words[j];
}
}
}
// read data files
for(int traintest = 0; traintest < 2; traintest++) {
ArrayList<int[]> ex_list = new ArrayList<int[]>();
ArrayList<Integer> lab_list = new ArrayList<Integer>();
if (traintest == 1)
open_file(filestem + ".train");
else
try {
open_file(filestem + ".test");
} catch (FileNotFoundException e) {
System.err.print("Continuing without test file...\n");
numTestExs = 0;
testEx = new int[0][];
continue;
}
while((line = read_line()) != null) {
line = line.trim( );
if (line.equals(""))
continue;
words = line.split("\\s+");
if (words.length != numAttrs + traintest) {
String err = "wrong number of tokens at line "
+ line_count + " in file " + filename;
System.err.println(err);
throw new RuntimeException(err);
}
int ex[] = new int[numAttrs];
for (int i = 0; i < numAttrs; i++) {
if (attrVals[i] == null) {
try {
ex[i] = Integer.parseInt(words[i]);
} catch (NumberFormatException e) {
System.err.println("Expected integer in field "
+(i+1)+" at line "+line_count+
" in file "+filename);
throw e;
}
} else {
int j;
for (j = 0;
j < attrVals[i].length
&& !attrVals[i][j].equals(words[i]);
j++);
if (j >= attrVals[i].length) {
String err = "bad attribute value in field "
+(i+1)+" at line "+line_count+" in file "+filename;
System.err.println(err);
throw new RuntimeException(err);
}
ex[i] = j;
}
}
ex_list.add(ex);
if (traintest == 1) {
int lab;
if (words[numAttrs].equals(className[0])) {
lab = 0;
} else if (words[numAttrs].equals(className[1])) {
lab = 1;
} else {
String err = "unrecognized label at line "+line_count+
" in file "+filename;
System.err.println(err);
throw new RuntimeException(err);
}
lab_list.add(new Integer(lab));
}
}
if (traintest == 0) {
numTestExs = ex_list.size();
testEx = new int[0][];
testEx = (int[][]) ex_list.toArray(testEx);
} else {
numTrainExs = ex_list.size();
trainEx = new int[0][];
trainEx = (int[][]) ex_list.toArray(trainEx);
trainLabel = new int[numTrainExs];
for (int i = 0; i < numTrainExs; i++) {
trainLabel[i] = (lab_list.get(i)).intValue();
}
}
in.close();
}
in = null;
filename = null;
}
/** This method prints out the predictions of classifier
* <tt>c</tt> on each of the test examples in the format required
* for submission. The result is sent to the given
* <tt>PrintStream</tt>.
**/
public void printTestPredictions(Classifier c,
PrintStream out) {
out.println(c.author());
out.println(".");
out.println(c.algorithmDescription());
out.println(".");
for(int i = 0; i < numTestExs; i++) {
out.println(className[c.predict(testEx[i])]);
}
}
/** This method prints out the predictions of classifier
* <tt>c</tt> on each of the test examples in the format required
* for submission. The result is printed to the file
* <tt>filestem.testout</tt>.
**/
public void printTestPredictions(Classifier c,
String filestem)
throws FileNotFoundException {
PrintStream out;
try {
out = new PrintStream(new BufferedOutputStream(new
FileOutputStream(filestem + ".testout")));
} catch (FileNotFoundException e) {
System.err.println("Cannot open file " + filestem + ".testout");
throw e;
}
printTestPredictions(c, out);
out.close();
}
/*********************** private ********************************/
private String filename;
private int line_count;
private BufferedReader in;
private void open_file(String filename) throws FileNotFoundException {
BufferedReader in;
this.filename = filename;
this.line_count = 0;
try {
in = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException e) {
System.err.print("File "+filename+" not found.\n");
throw e;
}
this.in = in;
}
private String read_line() throws IOException {
String line;
line_count++;
try {
line = in.readLine();
}
catch (IOException e) {
System.err.println("Error reading line "+line_count+" in file "+filename);
throw e;
}
return line;
}
protected int[][] getContVals() {
int[][] vals = new int[numAttrs][];
for (int a = 0; a < numAttrs; a++) {
if (attrVals[a] != null)
continue;
TreeSet<Integer> t = new TreeSet<Integer>();
for (int traintest = 0; traintest < 2; traintest++) {
int[][] exs = (traintest == 1 ? trainEx : testEx);
for (int i = 0; i < exs.length; i++) {
t.add(new Integer(exs[i][a]));
}
}
vals[a] = new int[t.size()];
Iterator<Integer> it = t.iterator();
int i = 0;
while(it.hasNext()) {
vals[a][i++] = (it.next()).intValue();
}
}
return vals;
}
}