View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math4.legacy.ml.clustering;
19  
20  
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  public class MultiKMeansPlusPlusClustererTest {
28  
29      @Test
30      public void dimension2() {
31          MultiKMeansPlusPlusClusterer<DoublePoint> transformer =
32              new MultiKMeansPlusPlusClusterer<>(
33                      new KMeansPlusPlusClusterer<>(3, 10), 5);
34  
35          DoublePoint[] points = new DoublePoint[] {
36  
37                  // first expected cluster
38                  new DoublePoint(new int[] { -15,  3 }),
39                  new DoublePoint(new int[] { -15,  4 }),
40                  new DoublePoint(new int[] { -15,  5 }),
41                  new DoublePoint(new int[] { -14,  3 }),
42                  new DoublePoint(new int[] { -14,  5 }),
43                  new DoublePoint(new int[] { -13,  3 }),
44                  new DoublePoint(new int[] { -13,  4 }),
45                  new DoublePoint(new int[] { -13,  5 }),
46  
47                  // second expected cluster
48                  new DoublePoint(new int[] { -1,  0 }),
49                  new DoublePoint(new int[] { -1, -1 }),
50                  new DoublePoint(new int[] {  0, -1 }),
51                  new DoublePoint(new int[] {  1, -1 }),
52                  new DoublePoint(new int[] {  1, -2 }),
53  
54                  // third expected cluster
55                  new DoublePoint(new int[] { 13,  3 }),
56                  new DoublePoint(new int[] { 13,  4 }),
57                  new DoublePoint(new int[] { 14,  4 }),
58                  new DoublePoint(new int[] { 14,  7 }),
59                  new DoublePoint(new int[] { 16,  5 }),
60                  new DoublePoint(new int[] { 16,  6 }),
61                  new DoublePoint(new int[] { 17,  4 }),
62                  new DoublePoint(new int[] { 17,  7 })
63          };
64          List<CentroidCluster<DoublePoint>> clusters = transformer.cluster(Arrays.asList(points));
65  
66          Assert.assertEquals(3, clusters.size());
67          boolean cluster1Found = false;
68          boolean cluster2Found = false;
69          boolean cluster3Found = false;
70          double epsilon = 1e-6;
71          for (CentroidCluster<DoublePoint> cluster : clusters) {
72              Clusterable center = cluster.getCenter();
73              double[] point = center.getPoint();
74              if (point[0] < 0) {
75                  cluster1Found = true;
76                  Assert.assertEquals(8, cluster.getPoints().size());
77                  Assert.assertEquals(-14, point[0], epsilon);
78                  Assert.assertEquals( 4, point[1], epsilon);
79              } else if (point[1] < 0) {
80                  cluster2Found = true;
81                  Assert.assertEquals(5, cluster.getPoints().size());
82                  Assert.assertEquals( 0, point[0], epsilon);
83                  Assert.assertEquals(-1, point[1], epsilon);
84              } else {
85                  cluster3Found = true;
86                  Assert.assertEquals(8, cluster.getPoints().size());
87                  Assert.assertEquals(15, point[0], epsilon);
88                  Assert.assertEquals(5, point[1], epsilon);
89              }
90          }
91          Assert.assertTrue(cluster1Found);
92          Assert.assertTrue(cluster2Found);
93          Assert.assertTrue(cluster3Found);
94      }
95  }