1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.stat.clustering;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Random;
26
27 import org.junit.Test;
28
29 public class KMeansPlusPlusClustererTest {
30
31 @Test
32 public void dimension2() {
33 KMeansPlusPlusClusterer<EuclideanIntegerPoint> transformer =
34 new KMeansPlusPlusClusterer<EuclideanIntegerPoint>(new Random(1746432956321l));
35 EuclideanIntegerPoint[] points = new EuclideanIntegerPoint[] {
36
37
38 new EuclideanIntegerPoint(new int[] { -15, 3 }),
39 new EuclideanIntegerPoint(new int[] { -15, 4 }),
40 new EuclideanIntegerPoint(new int[] { -15, 5 }),
41 new EuclideanIntegerPoint(new int[] { -14, 3 }),
42 new EuclideanIntegerPoint(new int[] { -14, 5 }),
43 new EuclideanIntegerPoint(new int[] { -13, 3 }),
44 new EuclideanIntegerPoint(new int[] { -13, 4 }),
45 new EuclideanIntegerPoint(new int[] { -13, 5 }),
46
47
48 new EuclideanIntegerPoint(new int[] { -1, 0 }),
49 new EuclideanIntegerPoint(new int[] { -1, -1 }),
50 new EuclideanIntegerPoint(new int[] { 0, -1 }),
51 new EuclideanIntegerPoint(new int[] { 1, -1 }),
52 new EuclideanIntegerPoint(new int[] { 1, -2 }),
53
54
55 new EuclideanIntegerPoint(new int[] { 13, 3 }),
56 new EuclideanIntegerPoint(new int[] { 13, 4 }),
57 new EuclideanIntegerPoint(new int[] { 14, 4 }),
58 new EuclideanIntegerPoint(new int[] { 14, 7 }),
59 new EuclideanIntegerPoint(new int[] { 16, 5 }),
60 new EuclideanIntegerPoint(new int[] { 16, 6 }),
61 new EuclideanIntegerPoint(new int[] { 17, 4 }),
62 new EuclideanIntegerPoint(new int[] { 17, 7 })
63
64 };
65 List<Cluster<EuclideanIntegerPoint>> clusters =
66 transformer.cluster(Arrays.asList(points), 3, 10);
67
68 assertEquals(3, clusters.size());
69 boolean cluster1Found = false;
70 boolean cluster2Found = false;
71 boolean cluster3Found = false;
72 for (Cluster<EuclideanIntegerPoint> cluster : clusters) {
73 int[] center = cluster.getCenter().getPoint();
74 if (center[0] < 0) {
75 cluster1Found = true;
76 assertEquals(8, cluster.getPoints().size());
77 assertEquals(-14, center[0]);
78 assertEquals( 4, center[1]);
79 } else if (center[1] < 0) {
80 cluster2Found = true;
81 assertEquals(5, cluster.getPoints().size());
82 assertEquals( 0, center[0]);
83 assertEquals(-1, center[1]);
84 } else {
85 cluster3Found = true;
86 assertEquals(8, cluster.getPoints().size());
87 assertEquals(15, center[0]);
88 assertEquals(5, center[1]);
89 }
90 }
91 assertTrue(cluster1Found);
92 assertTrue(cluster2Found);
93 assertTrue(cluster3Found);
94
95 }
96
97 }