-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrossValidation.java
More file actions
146 lines (112 loc) · 4.22 KB
/
CrossValidation.java
File metadata and controls
146 lines (112 loc) · 4.22 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
package LogisticRegression;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
/**
* Created by jay on 11/28/15.
*/
public class CrossValidation {
String pathToFile;
ArrayList<Example> foldTestList;
public CrossValidation(String pathToFile) {
this.pathToFile = pathToFile;
}
public String getPathToFile() {
return pathToFile;
}
public void setPathToFile(String pathToFile) {
this.pathToFile = pathToFile;
}
public ArrayList<Example> runCrossValidation(int folds, Data data, int foldIndex, boolean isCrossValidation) {
ArrayList<Example> allExamples = new ArrayList<Example>();
BufferedReader br;
Set<String> distinctLabels = new HashSet<String>();
Map<String, Integer> labelCount = new HashMap<>();
try {
br = new BufferedReader(new FileReader(pathToFile));
setDataSpecificInformation(data, br);
String line;
int index = 0;
while ((line = br.readLine()) != null) {
String perLine[] = line.split(",");
Example trainingExample = new Example();
trainingExample.setIndex(index);
double[] values = new double[perLine.length - 1];
for (int i = 0; i < perLine.length; i++) {
if (i == perLine.length - 1) {
trainingExample.setActualLabel(perLine[i]);
distinctLabels.add(perLine[i]);
addInMap(labelCount, perLine[i]);
} else {
values[i] = Double.parseDouble(perLine[i]);
}
}
index++;
trainingExample.setValues(values);
allExamples.add(trainingExample);
}
br.close();
data.setNumberOfClassLabels(distinctLabels.size());
data.setDistinctLabels(distinctLabels);
} catch (IOException e) {
e.printStackTrace();
}
return makeDataSet(allExamples, folds, foldIndex);
}
private void addInMap(Map<String, Integer> labelCount, String label) {
if (labelCount.containsKey(label)) {
Integer count = labelCount.get(label);
labelCount.put(label, ++count);
} else {
labelCount.put(label, 1);
}
}
private ArrayList<Example> makeDataSet(ArrayList<Example> allExamples, int folds, int foldIndex) {
if (folds == 1) {
return allExamples;
}
List<ArrayList<Example>> listOfSets = new ArrayList<ArrayList<Example>>();
divideSets(listOfSets, allExamples, folds);
this.foldTestList = listOfSets.get(foldIndex);
int trainIndex = 0;
ArrayList<Example> trainFold = new ArrayList<Example>();
for (ArrayList<Example> examples : listOfSets) {
if (trainIndex != foldIndex) {
trainFold.addAll(examples);
}
trainIndex++;
}
return trainFold;
}
private void divideSets(List<ArrayList<Example>> listOfSets, ArrayList<Example> allExamples, int folds) {
int foldSize = allExamples.size() / folds;
int start = 0;
int end = foldSize;
while (start < allExamples.size()) {
ArrayList<Example> foldList = new ArrayList<Example>();
for (int i = start; i < end; i++) {
foldList.add(allExamples.get(i));
}
listOfSets.add(foldList);
start = start + foldSize;
end = end + foldSize;
}
}
private static void setDataSpecificInformation(Data data, BufferedReader br) {
String str;
try {
str = br.readLine();
String[] perLine = str.split(",");
data.setNumberOfCols(perLine.length - 1);
String[] colNames = new String[perLine.length - 1];
for (int j = 0; j < perLine.length - 1; j++) {
colNames[j] = perLine[j];
}
data.setNamesOfColumns(colNames);
} catch (IOException e) {
e.printStackTrace();
}
}
}