/
HierarchicalAgglomerative.java
executable file
·588 lines (487 loc) · 21.3 KB
/
HierarchicalAgglomerative.java
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/**
* Copyright (C) 2013-2016 Vasilis Vryniotis <bbriniotis@datumbox.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datumbox.framework.core.machinelearning.clustering;
import com.datumbox.framework.common.Configuration;
import com.datumbox.framework.common.concurrency.ForkJoinStream;
import com.datumbox.framework.common.concurrency.StreamMethods;
import com.datumbox.framework.common.dataobjects.AssociativeArray;
import com.datumbox.framework.common.dataobjects.Dataframe;
import com.datumbox.framework.common.dataobjects.Record;
import com.datumbox.framework.common.persistentstorage.interfaces.DatabaseConnector;
import com.datumbox.framework.common.persistentstorage.interfaces.DatabaseConnector.MapType;
import com.datumbox.framework.common.persistentstorage.interfaces.DatabaseConnector.StorageHint;
import com.datumbox.framework.common.utilities.MapMethods;
import com.datumbox.framework.core.machinelearning.common.abstracts.AbstractTrainer;
import com.datumbox.framework.core.machinelearning.common.abstracts.modelers.AbstractClusterer;
import com.datumbox.framework.core.machinelearning.common.interfaces.PredictParallelizable;
import com.datumbox.framework.core.machinelearning.common.interfaces.TrainParallelizable;
import com.datumbox.framework.core.mathematics.distances.Distance;
import com.datumbox.framework.core.statistics.descriptivestatistics.Descriptives;
import java.util.*;
/**
* This class implements the Hierarchical Agglomerative clustering algorithm
* supporting different Linkage and Distance methods.
*
* References:
* http://nlp.stanford.edu/IR-book/html/htmledition/hierarchical-agglomerative-clustering-1.html
* http://php-nlp-tools.com/posts/faster-hierarchical-clustering.html
*
* @author Vasilis Vryniotis <bbriniotis@datumbox.com>
*/
public class HierarchicalAgglomerative extends AbstractClusterer<HierarchicalAgglomerative.Cluster, HierarchicalAgglomerative.ModelParameters, HierarchicalAgglomerative.TrainingParameters> implements PredictParallelizable, TrainParallelizable {
/** {@inheritDoc} */
public static class Cluster extends AbstractClusterer.AbstractCluster {
private static final long serialVersionUID = 1L;
private Record centroid;
private boolean active = true;
private final AssociativeArray xi_sum;
/**
* @param clusterId
* @see AbstractCluster#AbstractCluster(java.lang.Integer)
*/
protected Cluster(int clusterId) {
super(clusterId);
centroid = new Record(new AssociativeArray(), null);
xi_sum = new AssociativeArray();
}
/**
* Returns the centroid of the cluster.
*
* @return
*/
public Record getCentroid() {
return centroid;
}
/**
* Merges this cluster with the provided cluster.
*
* @param c
*/
protected void merge(Cluster c) {
xi_sum.addValues(c.xi_sum);
size += c.size;
}
/**
* Updates the cluster parameters.
*
* @return
*/
protected boolean updateClusterParameters() {
boolean changed=false;
AssociativeArray centoidValues = xi_sum.copy();
if(size>0) {
centoidValues.multiplyValues(1.0/size);
}
if(!centroid.getX().equals(centoidValues)) {
changed=true;
centroid = new Record(centoidValues, centroid.getY());
}
return changed;
}
/**
* Getter for whether the cluster is active.
*
* @return
*/
protected boolean isActive() {
return active;
}
/**
* Setter for whether the cluster is active.
*
* @param active
*/
protected void setActive(boolean active) {
this.active = active;
}
/** {@inheritDoc} */
@Override
protected void add(Record r) {
size++;
xi_sum.addValues(r.getX());
}
/** {@inheritDoc} */
@Override
protected void remove(Record r) {
throw new UnsupportedOperationException("Remove operation is not supported.");
}
/** {@inheritDoc} */
@Override
protected void clear() {
xi_sum.clear();
}
}
/** {@inheritDoc} */
public static class ModelParameters extends AbstractClusterer.AbstractModelParameters<HierarchicalAgglomerative.Cluster> {
private static final long serialVersionUID = 1L;
/**
* @param dbc
* @see AbstractTrainer.AbstractModelParameters#AbstractModelParameters(DatabaseConnector)
*/
protected ModelParameters(DatabaseConnector dbc) {
super(dbc);
}
}
/** {@inheritDoc} */
public static class TrainingParameters extends AbstractClusterer.AbstractTrainingParameters {
private static final long serialVersionUID = 1L;
/**
* The Linkage method used in the calculations.
*/
public enum Linkage {
/**
* Average Linkage (all points to all points).
*/
AVERAGE,
/**
* Nearest Neighbour.
*/
SINGLE,
/**
* Farther Neighbour.
*/
COMPLETE;
}
/**
* The Distance method used in the calculations.
*/
public enum Distance {
/**
* Euclidian distance.
*/
EUCLIDIAN,
/**
* Manhattan distance.
*/
MANHATTAN,
/**
* Maximum distance.
*/
MAXIMUM;
}
//Vars
private Linkage linkageMethod = Linkage.COMPLETE;
private Distance distanceMethod = Distance.EUCLIDIAN;
private double maxDistanceThreshold = Double.MAX_VALUE;
private double minClustersThreshold = 2;
//Getters Setters
/**
* Getter for Linkage Method.
*
* @return
*/
public Linkage getLinkageMethod() {
return linkageMethod;
}
/**
* Setter for Linkage Method.
*
* @param linkageMethod
*/
public void setLinkageMethod(Linkage linkageMethod) {
this.linkageMethod = linkageMethod;
}
/**
* Getter for Distance Method.
*
* @return
*/
public Distance getDistanceMethod() {
return distanceMethod;
}
/**
* Setter for Distance Method.
*
* @param distanceMethod
*/
public void setDistanceMethod(Distance distanceMethod) {
this.distanceMethod = distanceMethod;
}
/**
* Getter for the maximum distance threshold.
*
* @return
*/
public double getMaxDistanceThreshold() {
return maxDistanceThreshold;
}
/**
* Setter for the maximum distance threshold.
*
* @param maxDistanceThreshold
*/
public void setMaxDistanceThreshold(double maxDistanceThreshold) {
this.maxDistanceThreshold = maxDistanceThreshold;
}
/**
* Getter for the minimum clusters threshold.
*
* @return
*/
public double getMinClustersThreshold() {
return minClustersThreshold;
}
/**
* Setter for the minimum clusters threshold.
*
* @param minClustersThreshold
*/
public void setMinClustersThreshold(double minClustersThreshold) {
this.minClustersThreshold = minClustersThreshold;
}
}
/**
* Public constructor of the algorithm.
*
* @param dbName
* @param conf
*/
public HierarchicalAgglomerative(String dbName, Configuration conf) {
super(dbName, conf, HierarchicalAgglomerative.ModelParameters.class, HierarchicalAgglomerative.TrainingParameters.class);
streamExecutor = new ForkJoinStream(knowledgeBase.getConf().getConcurrencyConfig());
}
private boolean parallelized = true;
/**
* This executor is used for the parallel processing of streams with custom
* Thread pool.
*/
protected final ForkJoinStream streamExecutor;
/** {@inheritDoc} */
@Override
public boolean isParallelized() {
return parallelized;
}
/** {@inheritDoc} */
@Override
public void setParallelized(boolean parallelized) {
this.parallelized = parallelized;
}
/** {@inheritDoc} */
@Override
protected void _predictDataset(Dataframe newData) {
DatabaseConnector dbc = knowledgeBase.getDbc();
Map<Integer, Prediction> resultsBuffer = dbc.getBigMap("tmp_resultsBuffer", Integer.class, Prediction.class, MapType.HASHMAP, StorageHint.IN_DISK, true, true);
_predictDatasetParallel(newData, resultsBuffer, knowledgeBase.getConf().getConcurrencyConfig());
dbc.dropBigMap("tmp_resultsBuffer", resultsBuffer);
}
/** {@inheritDoc} */
@Override
public Prediction _predictRecord(Record r) {
ModelParameters modelParameters = knowledgeBase.getModelParameters();
Map<Integer, Cluster> clusterMap = modelParameters.getClusterMap();
AssociativeArray clusterDistances = new AssociativeArray();
for(Map.Entry<Integer, Cluster> e : clusterMap.entrySet()) {
Integer clusterId = e.getKey();
Cluster c = e.getValue();
double distance = calculateDistance(r, c.getCentroid());
clusterDistances.put(clusterId, distance);
}
Descriptives.normalize(clusterDistances);
return new Prediction(getSelectedClusterFromDistances(clusterDistances), clusterDistances);
}
/** {@inheritDoc} */
@Override
protected void _fit(Dataframe trainingData) {
ModelParameters modelParameters = knowledgeBase.getModelParameters();
Set<Object> goldStandardClasses = modelParameters.getGoldStandardClasses();
//check if there are any gold standard classes
for(Record r : trainingData) {
Object theClass=r.getY();
if(theClass!=null) {
goldStandardClasses.add(theClass);
}
}
//calculate clusters
calculateClusters(trainingData);
clearClusters();
}
private double calculateDistance(Record r1, Record r2) {
TrainingParameters trainingParameters = knowledgeBase.getTrainingParameters();
double distance = 0.0;
TrainingParameters.Distance distanceMethod = trainingParameters.getDistanceMethod();
if(distanceMethod==TrainingParameters.Distance.EUCLIDIAN) {
distance = Distance.euclidean(r1.getX(), r2.getX());
}
else if(distanceMethod==TrainingParameters.Distance.MANHATTAN) {
distance = Distance.manhattan(r1.getX(), r2.getX());
}
else if(distanceMethod==TrainingParameters.Distance.MAXIMUM) {
distance = Distance.maximum(r1.getX(), r2.getX());
}
else {
throw new IllegalArgumentException("Unsupported Distance method.");
}
return distance;
}
private Object getSelectedClusterFromDistances(AssociativeArray clusterDistances) {
Map.Entry<Object, Object> minEntry = MapMethods.selectMinKeyValue(clusterDistances);
return minEntry.getKey();
}
private void calculateClusters(Dataframe trainingData) {
ModelParameters modelParameters = knowledgeBase.getModelParameters();
TrainingParameters trainingParameters = knowledgeBase.getTrainingParameters();
Map<Integer, Cluster> clusterMap = modelParameters.getClusterMap();
DatabaseConnector dbc = knowledgeBase.getDbc();
Map<List<Object>, Double> tmp_distanceArray = dbc.getBigMap("tmp_distanceArray", (Class<List<Object>>)(Class<?>)List.class, Double.class, MapType.HASHMAP, StorageHint.IN_CACHE, true, true); //it holds the distances between clusters
Map<Integer, Integer> tmp_minClusterDistanceId = dbc.getBigMap("tmp_minClusterDistanceId", Integer.class, Integer.class, MapType.HASHMAP, StorageHint.IN_CACHE, true, true); //it holds the ids of the min distances
//initialize clusters, foreach point create a cluster
Integer clusterId = 0;
for(Record r : trainingData.values()) {
Cluster c = new Cluster(clusterId);
c.add(r);
c.updateClusterParameters();
clusterMap.put(clusterId, c);
++clusterId;
}
//calculate distance table and minimum distances
streamExecutor.forEach(StreamMethods.stream(clusterMap.entrySet().stream(), isParallelized()), entry1 -> {
Integer clusterId1 = entry1.getKey();
Cluster c1 = entry1.getValue();
for(Map.Entry<Integer, Cluster> entry2 : clusterMap.entrySet()) {
Integer clusterId2 = entry2.getKey();
Cluster c2 = entry2.getValue();
double distance = Double.MAX_VALUE;
if(!Objects.equals(clusterId1, clusterId2)) {
distance = calculateDistance(c1.getCentroid(), c2.getCentroid());
}
tmp_distanceArray.put(Arrays.asList(clusterId1, clusterId2), distance);
tmp_distanceArray.put(Arrays.asList(clusterId2, clusterId1), distance);
Integer minDistanceId = tmp_minClusterDistanceId.get(clusterId1);
if(minDistanceId==null || distance < tmp_distanceArray.get(Arrays.asList(clusterId1, minDistanceId))) {
tmp_minClusterDistanceId.put(clusterId1, clusterId2);
}
}
});
//merging process
boolean continueMerging = true;
while(continueMerging) {
continueMerging = mergeClosest(tmp_minClusterDistanceId, tmp_distanceArray);
//count all the active clusters
int activeClusters = 0;
for(Cluster c : clusterMap.values()) {
if(c.isActive()) {
++activeClusters;
}
}
if(activeClusters<=trainingParameters.getMinClustersThreshold()) {
continueMerging = false;
}
}
//update centroids. it does not update their IDs
Iterator<Map.Entry<Integer, Cluster>> it = clusterMap.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<Integer, Cluster> entry = it.next();
Integer cId = entry.getKey();
Cluster cluster = entry.getValue();
if(cluster.isActive()) {
cluster.updateClusterParameters();
clusterMap.put(cId, cluster);
}
else {
it.remove(); //remove inactive clusters
}
}
//Drop the temporary Collection
dbc.dropBigMap("tmp_distanceArray", tmp_distanceArray);
dbc.dropBigMap("tmp_minClusterDistanceId", tmp_minClusterDistanceId);
}
private boolean mergeClosest(Map<Integer, Integer> minClusterDistanceId, Map<List<Object>, Double> distanceArray) {
ModelParameters modelParameters = knowledgeBase.getModelParameters();
TrainingParameters trainingParameters = knowledgeBase.getTrainingParameters();
Map<Integer, Cluster> clusterMap = modelParameters.getClusterMap();
//find the two closest clusters
Integer minClusterId = null;
double minDistance = Double.MAX_VALUE;
for(Map.Entry<Integer, Cluster> entry : clusterMap.entrySet()) {
Integer clusterId = entry.getKey();
if(entry.getValue().isActive()==false) {
continue; //skip inactive clusters
}
double distance = distanceArray.get(Arrays.asList(clusterId, minClusterDistanceId.get(clusterId)));
if(distance<minDistance) {
minClusterId = clusterId;
minDistance = distance;
}
}
if(minDistance>=trainingParameters.getMaxDistanceThreshold()) {
return false;
}
final Integer clusterThatMergesId = minClusterId;
final Integer clusterToBeMergedId = minClusterDistanceId.get(clusterThatMergesId);
Cluster c1 = clusterMap.get(clusterThatMergesId);
Cluster c2 = clusterMap.get(clusterToBeMergedId);
double c1Size = c1.size();
double c2Size = c2.size();
//merge together the two closest clusters
c1.merge(c2);
clusterMap.put(clusterThatMergesId, c1);
c2.setActive(false); //set the cluster that we just merged inactive
clusterMap.put(clusterToBeMergedId, c2);
//update the distances with the merged cluster
TrainingParameters.Linkage linkageMethod = trainingParameters.getLinkageMethod();
streamExecutor.forEach(StreamMethods.stream(clusterMap.entrySet().stream(), isParallelized()), entry -> {
Integer clusterId = entry.getKey();
Cluster ci = entry.getValue();
if(ci.isActive()) { //skip inactive clusters
double distance;
if(Objects.equals(clusterThatMergesId, clusterId)) {
distance = Double.MAX_VALUE;
}
else if(linkageMethod==TrainingParameters.Linkage.SINGLE) {
double c1ciDistance = distanceArray.get(Arrays.asList(clusterThatMergesId, clusterId));
double c2ciDistance = distanceArray.get(Arrays.asList(clusterToBeMergedId, clusterId));
distance = Math.min(c1ciDistance, c2ciDistance);
}
else if(linkageMethod==TrainingParameters.Linkage.COMPLETE) {
double c1ciDistance = distanceArray.get(Arrays.asList(clusterThatMergesId, clusterId));
double c2ciDistance = distanceArray.get(Arrays.asList(clusterToBeMergedId, clusterId));
distance = Math.max(c1ciDistance, c2ciDistance);
}
else if(linkageMethod==TrainingParameters.Linkage.AVERAGE) {
double c1ciDistance = distanceArray.get(Arrays.asList(clusterThatMergesId, clusterId));
double c2ciDistance = distanceArray.get(Arrays.asList(clusterToBeMergedId, clusterId));
distance = (c1ciDistance*c1Size + c2ciDistance*c2Size)/(c1Size+c2Size);
}
else {
distance = calculateDistance(c1.getCentroid(), ci.getCentroid());
}
distanceArray.put(Arrays.asList(clusterThatMergesId, clusterId), distance);
distanceArray.put(Arrays.asList(clusterId, clusterThatMergesId), distance);
}
});
//update minimum distance ids
streamExecutor.forEach(StreamMethods.stream(clusterMap.entrySet().stream(), isParallelized()), entry1 -> {
Integer id1 = entry1.getKey();
if (entry1.getValue().isActive()) { //skip inactive clusters
Integer minDistanceId = minClusterDistanceId.get(id1);
if (Objects.equals(minDistanceId, clusterThatMergesId) || Objects.equals(minDistanceId, clusterToBeMergedId)) {
Integer newMinDistanceId = id1;
for(Map.Entry<Integer, Cluster> entry2 : clusterMap.entrySet()) {
Integer id2 = entry2.getKey();
if(entry2.getValue().isActive()==false) {
continue; //skip inactive clusters
}
if(distanceArray.get(Arrays.asList(id1, id2)) < distanceArray.get(Arrays.asList(id1, newMinDistanceId)) ){
newMinDistanceId = id2;
}
}
minClusterDistanceId.put(id1, newMinDistanceId);
}
}
});
return true;
}
}