-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTester.java
More file actions
134 lines (102 loc) · 3.76 KB
/
Tester.java
File metadata and controls
134 lines (102 loc) · 3.76 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
package nervousNet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.concurrent.ThreadLocalRandom;
import nervousnet.challenge.Dumper;
import nervousnet.challenge.Loader;
import nervousnet.challenge.exceptions.IllegalHashMapArgumentException;
import nervousnet.challenge.exceptions.MissingDataException;
import nervousnet.challenge.exceptions.MissingFileException;
import nervousnet.challenge.exceptions.NullArgumentException;
import nervousnet.challenge.exceptions.UnsupportedRawFileFormatException;
import nervousnet.challenge.tags.Tags;
import weka.clusterers.SimpleKMeans;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
@SuppressWarnings("unused")
public class Tester {
private Loader loader = new Loader();
private Dumper dumper = new Dumper();
public int numOfClusters = 2;
public Tester() {
}
private ArrayList<Double> kMeans(ArrayList<Double> raws, int numOfClusters) throws Exception {
FastVector atts = new FastVector();
atts.addElement(new Attribute("NN"));
Instances kmeans = new Instances("kmeans", atts, 0);
double epsilon = 2; // Epsilon>0
Double[] keys = new Double[raws.size()];
Double[] keys2 = new Double[raws.size()];
double sum = 0.0;
for (int i=0; i<raws.size(); i++) {
keys[i] = ThreadLocalRandom.current().nextDouble(0, 1);
sum += keys[i];
}
for (int i=0; i<raws.size(); i++) {
keys2[i] = keys[i]/sum*epsilon;
}
int j=0;
for(Double raw : raws) {
double vals[] = new double[kmeans.numAttributes()];
//double modifiedValue = ThreadLocalRandom.current().nextDouble(Math.max(0, raw-epsilon), raw+epsilon);
vals[0] = raw + keys2[j];
j++;
//vals[0] = modifiedValue;
kmeans.add(new Instance(1.0, vals));
}
SimpleKMeans kMeansAlgo = new SimpleKMeans();
kMeansAlgo.setSeed(10);
kMeansAlgo.setPreserveInstancesOrder(true);
kMeansAlgo.setNumClusters(numOfClusters);
kMeansAlgo.buildClusterer(kmeans);
int[] assignments = kMeansAlgo.getAssignments();
Instances centroids = kMeansAlgo.getClusterCentroids();
ArrayList<Double> centroidList = new ArrayList<Double>();
for(int i = 0; i < assignments.length; i++) {
double rawValue = kmeans.instance(i).value(0);
double centroidValue = centroids.instance(assignments[i]).value(0);
centroidList.add(centroidValue);
}
return centroidList;
}
public void analyze() {
try {
HashMap<Integer, LinkedHashMap<Integer, LinkedHashMap<Integer, Double>>> rawMap = loader.exportRawValues();
HashMap<Integer, LinkedHashMap<Integer, LinkedHashMap<Integer, Double>>> outputMap = Dumper.initOutputMap();
System.out.println("Processing data..");
for(Integer user : loader.getSortedUsers()) {
for(Integer day : loader.getSortedDays(user)) {
try {
ArrayList<Double> rawValues = loader.getSortedRawValues(user, day);
ArrayList<Double> summarizedValues = this.kMeans(rawValues, numOfClusters);
for(int time = Tags.minTime, i = 0; time <= Tags.maxTime; time++, i++) {
outputMap.get(user).get(day).put(time, summarizedValues.get(i));
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
dumper.dump(outputMap);
} /*catch (UnsupportedRawFileFormatException e) {
e.printStackTrace();
} */catch (IllegalHashMapArgumentException e) {
e.printStackTrace();
} catch (NullArgumentException e) {
e.printStackTrace();
} catch (MissingDataException e) {
e.printStackTrace();
} catch (MissingFileException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Tester tester = new Tester();
String numOfClusters = "16";
tester.numOfClusters = Integer.parseInt(numOfClusters);
tester.analyze();
}
}