1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.ml.clustering.evaluation;
19
20 import java.util.List;
21
22 import org.apache.commons.math4.legacy.ml.clustering.Cluster;
23 import org.apache.commons.math4.legacy.ml.clustering.Clusterable;
24 import org.apache.commons.math4.legacy.ml.clustering.ClusterEvaluator;
25 import org.apache.commons.math4.legacy.ml.distance.DistanceMeasure;
26 import org.apache.commons.math4.legacy.stat.descriptive.moment.Variance;
27
28
29
30
31
32
33
34
35
36
37
38 public class SumOfClusterVariances implements ClusterEvaluator {
39
40 private final DistanceMeasure measure;
41
42
43
44
45 public SumOfClusterVariances(final DistanceMeasure measure) {
46 this.measure = measure;
47 }
48
49
50 @Override
51 public double score(List<? extends Cluster<? extends Clusterable>> clusters) {
52 double varianceSum = 0.0;
53 for (final Cluster<? extends Clusterable> cluster : clusters) {
54 if (!cluster.getPoints().isEmpty()) {
55
56 final Clusterable center = cluster.centroid();
57
58
59 final Variance stat = new Variance();
60 for (final Clusterable point : cluster.getPoints()) {
61 stat.increment(distance(point, center));
62 }
63
64 varianceSum += stat.getResult();
65 }
66 }
67 return varianceSum;
68 }
69
70
71 @Override
72 public boolean isBetterScore(double a,
73 double b) {
74 return a < b;
75 }
76
77
78
79
80
81
82
83
84
85 private double distance(final Clusterable p1, final Clusterable p2) {
86 return measure.compute(p1.getPoint(), p2.getPoint());
87 }
88 }