-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestData.java
More file actions
69 lines (54 loc) · 1.8 KB
/
TestData.java
File metadata and controls
69 lines (54 loc) · 1.8 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
package LogisticRegression;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* Created by jay on 11/29/15.
*/
public class TestData {
private String pathToFile;
public Map<String, Integer> testLabelCount;
public TestData(String pathToFile) {
this.pathToFile = pathToFile;
this.testLabelCount = new HashMap<>();
}
public ArrayList<Example> testData() {
ArrayList<Example> test = new ArrayList<Example>();
BufferedReader br;
try {
br = new BufferedReader(new FileReader(pathToFile));
String line;
String ignoreLine = br.readLine();
int index = 0;
while ((line = br.readLine()) != null) {
String perLine[] = line.split(",");
Example testExample = new Example();
testExample.setIndex(index);
double[] values = new double[perLine.length - 1];
for (int i = 0; i < perLine.length; i++) {
if (i == perLine.length - 1) {
testExample.setActualLabel(perLine[i]);
addInMap(perLine[i]);
} else {
values[i] = Double.parseDouble(perLine[i]);
}
}
index++;
testExample.setValues(values);
test.add(testExample);
}
} catch (IOException e) {
e.printStackTrace();
}
return test;
}
private void addInMap(String label) {
if (testLabelCount.containsKey(label)) {
Integer count = testLabelCount.get(label);
testLabelCount.put(label, ++count);
} else {
testLabelCount.put(label, 1);
}
}
}