-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFolds.java
More file actions
74 lines (67 loc) · 2.35 KB
/
Copy pathFolds.java
File metadata and controls
74 lines (67 loc) · 2.35 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
package utils;
import java.io.File;
import java.io.IOException;
import java.util.*;
import org.apache.commons.io.FileUtils;
import structure.Problem;
import structure.StanfordProblem;
public class Folds {
public static List<Integer> readFoldIndices(String foldFile) throws IOException {
String str = FileUtils.readFileToString(new File(foldFile));
List<Integer> foldIndices = new ArrayList<>();
for(String index : str.split("\n")) {
foldIndices.add(Integer.parseInt(index));
}
return foldIndices;
}
// Returns List of 3 elements : train, val, test
public static List<List<Problem>> getDataSplit(
List<Problem> probs, List<Integer> trainIndices,
List<Integer> testIndices, double validationFrac) throws Exception {
List<Problem> allTrain = new ArrayList<>();
List<Problem> train = new ArrayList<>();
List<Problem> val = new ArrayList<>();
List<Problem> test = new ArrayList<>();
for(Problem prob : probs) {
if(testIndices.contains(prob.id)) {
test.add(prob);
}
if(trainIndices.contains(prob.id)) {
allTrain.add(prob);
}
}
Collections.shuffle(allTrain, new Random(0));
val.addAll(allTrain.subList(0, (int)(validationFrac*allTrain.size())));
train.addAll(allTrain.subList((int)(validationFrac*allTrain.size()), allTrain.size()));
List<List<Problem>> splits = new ArrayList<>();
splits.add(train);
splits.add(val);
splits.add(test);
return splits;
}
// Returns List of 3 elements : train, val, test
public static List<List<StanfordProblem>> getDataSplitForStanford(
List<StanfordProblem> probs, List<Integer> trainIndices,
List<Integer> testIndices, double validationFrac) throws Exception {
List<StanfordProblem> allTrain = new ArrayList<>();
List<StanfordProblem> train = new ArrayList<>();
List<StanfordProblem> val = new ArrayList<>();
List<StanfordProblem> test = new ArrayList<>();
for(StanfordProblem prob : probs) {
if(testIndices.contains(prob.id)) {
test.add(prob);
}
if(trainIndices.contains(prob.id)) {
allTrain.add(prob);
}
}
Collections.shuffle(allTrain, new Random(0));
val.addAll(allTrain.subList(0, (int)(validationFrac*allTrain.size())));
train.addAll(allTrain.subList((int)(validationFrac*allTrain.size()), allTrain.size()));
List<List<StanfordProblem>> splits = new ArrayList<>();
splits.add(train);
splits.add(val);
splits.add(test);
return splits;
}
}