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;
24
25
26 import java.util.Arrays;
27 import java.util.List;
28
29 import org.junit.Assert;
30 import org.junit.Test;
31
32 public class MultiKMeansPlusPlusClustererTest {
33
34 @Test
35 public void dimension2() {
36 MultiKMeansPlusPlusClusterer<DoublePoint> transformer =
37 new MultiKMeansPlusPlusClusterer<DoublePoint>(
38 new KMeansPlusPlusClusterer<DoublePoint>(3, 10), 5);
39
40 DoublePoint[] points = new DoublePoint[] {
41
42
43 new DoublePoint(new int[] { -15, 3 }),
44 new DoublePoint(new int[] { -15, 4 }),
45 new DoublePoint(new int[] { -15, 5 }),
46 new DoublePoint(new int[] { -14, 3 }),
47 new DoublePoint(new int[] { -14, 5 }),
48 new DoublePoint(new int[] { -13, 3 }),
49 new DoublePoint(new int[] { -13, 4 }),
50 new DoublePoint(new int[] { -13, 5 }),
51
52
53 new DoublePoint(new int[] { -1, 0 }),
54 new DoublePoint(new int[] { -1, -1 }),
55 new DoublePoint(new int[] { 0, -1 }),
56 new DoublePoint(new int[] { 1, -1 }),
57 new DoublePoint(new int[] { 1, -2 }),
58
59
60 new DoublePoint(new int[] { 13, 3 }),
61 new DoublePoint(new int[] { 13, 4 }),
62 new DoublePoint(new int[] { 14, 4 }),
63 new DoublePoint(new int[] { 14, 7 }),
64 new DoublePoint(new int[] { 16, 5 }),
65 new DoublePoint(new int[] { 16, 6 }),
66 new DoublePoint(new int[] { 17, 4 }),
67 new DoublePoint(new int[] { 17, 7 })
68
69 };
70 List<CentroidCluster<DoublePoint>> clusters = transformer.cluster(Arrays.asList(points));
71
72 Assert.assertEquals(3, clusters.size());
73 boolean cluster1Found = false;
74 boolean cluster2Found = false;
75 boolean cluster3Found = false;
76 double epsilon = 1e-6;
77 for (CentroidCluster<DoublePoint> cluster : clusters) {
78 Clusterable center = cluster.getCenter();
79 double[] point = center.getPoint();
80 if (point[0] < 0) {
81 cluster1Found = true;
82 Assert.assertEquals(8, cluster.getPoints().size());
83 Assert.assertEquals(-14, point[0], epsilon);
84 Assert.assertEquals( 4, point[1], epsilon);
85 } else if (point[1] < 0) {
86 cluster2Found = true;
87 Assert.assertEquals(5, cluster.getPoints().size());
88 Assert.assertEquals( 0, point[0], epsilon);
89 Assert.assertEquals(-1, point[1], epsilon);
90 } else {
91 cluster3Found = true;
92 Assert.assertEquals(8, cluster.getPoints().size());
93 Assert.assertEquals(15, point[0], epsilon);
94 Assert.assertEquals(5, point[1], epsilon);
95 }
96 }
97 Assert.assertTrue(cluster1Found);
98 Assert.assertTrue(cluster2Found);
99 Assert.assertTrue(cluster3Found);
100
101 }
102
103 }