- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataReader.java
More file actions
Latest commit
73 lines (56 loc) · 1.86 KB
/
Copy pathDataReader.java
File metadata and controls
73 lines (56 loc) · 1.86 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
importjava.io.BufferedInputStream;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
publicclassDataReader {
privateScanner_scanner;
// Classification or regression?
privateboolean_classification;
publicDataReader(Stringfilename, booleanclassification) throwsFileNotFoundException {
this._scanner = newScanner(newBufferedInputStream(newFileInputStream(filename)));
this._classification = classification;
}
publicvoidclose() {
this._scanner.close();
}
publicList<Instance> readData() {
ArrayList<Instance> instances = newArrayList<Instance>();
while (this._scanner.hasNextLine()) {
Stringline = this._scanner.nextLine();
if (line.trim().length() == 0)
continue;
//The Original: FeatureVector feature_vector = new FeatureVector();
// Divide the line into features and label.
String[] split_line = line.split(" ");
//My modification
FeatureVectorfeature_vector = newFeatureVector(split_line.length-1);
Stringlabel_string = split_line[0];
Labellabel = null;
if (this._classification) {
intint_label = Integer.parseInt(label_string);
if (int_label != -1) {
label = newClassificationLabel(int_label);
}
} else {
try {
doubledouble_label = Double.parseDouble(label_string);
label = newRegressionLabel(double_label);
} catch (Exceptione) {
}
}
for (intii = 1; ii < split_line.length; ii++) {
Stringitem = split_line[ii];
Stringname = item.split(":")[0];
intindex = Integer.parseInt(name);
doublevalue = Double.parseDouble(item.split(":")[1]);
if (value != 0)
feature_vector.add(index, value);
}
Instanceinstance = newInstance(feature_vector, label);
instances.add(instance);
}
returninstances;
}
}