1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.clustering.evaluation;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.hipparchus.clustering.Cluster;
33 import org.hipparchus.clustering.DoublePoint;
34 import org.hipparchus.clustering.distance.EuclideanDistance;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 public class SumOfClusterVariancesTest {
39
40 private ClusterEvaluator<DoublePoint> evaluator;
41
42 @Before
43 public void setUp() {
44 evaluator = new SumOfClusterVariances<DoublePoint>(new EuclideanDistance());
45 }
46
47 @Test
48 public void testScore() {
49 final DoublePoint[] points1 = new DoublePoint[] {
50 new DoublePoint(new double[] { 1 }),
51 new DoublePoint(new double[] { 2 }),
52 new DoublePoint(new double[] { 3 })
53 };
54
55 final DoublePoint[] points2 = new DoublePoint[] {
56 new DoublePoint(new double[] { 1 }),
57 new DoublePoint(new double[] { 5 }),
58 new DoublePoint(new double[] { 10 })
59 };
60
61 final List<Cluster<DoublePoint>> clusters = new ArrayList<Cluster<DoublePoint>>();
62
63 final Cluster<DoublePoint> cluster1 = new Cluster<DoublePoint>();
64 for (DoublePoint p : points1) {
65 cluster1.addPoint(p);
66 }
67 clusters.add(cluster1);
68
69 assertEquals(1.0/3.0, evaluator.score(clusters), 1e-6);
70
71 final Cluster<DoublePoint> cluster2 = new Cluster<DoublePoint>();
72 for (DoublePoint p : points2) {
73 cluster2.addPoint(p);
74 }
75 clusters.add(cluster2);
76
77 assertEquals(6.148148148, evaluator.score(clusters), 1e-6);
78 }
79
80 @Test
81 public void testOrdering() {
82 assertTrue(evaluator.isBetterScore(10, 20));
83 assertFalse(evaluator.isBetterScore(20, 1));
84 }
85 }