1 /*
2  * This file is part of ELKI:
3  * Environment for Developing KDD-Applications Supported by Index-Structures
4  *
5  * Copyright (C) 2018
6  * ELKI Development Team
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 package de.lmu.ifi.dbs.elki.algorithm.clustering.kmeans.initialization;
22 
23 import org.junit.Test;
24 
25 import de.lmu.ifi.dbs.elki.algorithm.clustering.AbstractClusterAlgorithmTest;
26 import de.lmu.ifi.dbs.elki.algorithm.clustering.kmeans.CLARA;
27 import de.lmu.ifi.dbs.elki.algorithm.clustering.kmeans.KMeans;
28 import de.lmu.ifi.dbs.elki.algorithm.clustering.kmeans.SingleAssignmentKMeans;
29 import de.lmu.ifi.dbs.elki.data.Clustering;
30 import de.lmu.ifi.dbs.elki.data.DoubleVector;
31 import de.lmu.ifi.dbs.elki.database.Database;
32 import de.lmu.ifi.dbs.elki.utilities.ELKIBuilder;
33 
34 /**
35  * Performs a single assignment with different k-means initializations.
36  *
37  * @author Erich Schubert
38  * @since 0.7.5
39  */
40 public class KMeansPlusPlusInitialMeansTest extends AbstractClusterAlgorithmTest {
41   /**
42    * Run KMeans with fixed parameters and compare the result to a golden
43    * standard.
44    */
45   @Test
testSingleAssignmentKMeansPlusPlus()46   public void testSingleAssignmentKMeansPlusPlus() {
47     Database db = makeSimpleDatabase(UNITTEST + "different-densities-2d-no-noise.ascii", 1000);
48     Clustering<?> result = new ELKIBuilder<SingleAssignmentKMeans<DoubleVector>>(SingleAssignmentKMeans.class) //
49         .with(KMeans.K_ID, 5) //
50         .with(KMeans.SEED_ID, 3) //
51         .with(KMeans.INIT_ID, KMeansPlusPlusInitialMeans.class) //
52         .build().run(db);
53     testFMeasure(db, result, 0.99205);
54     testClusterSizes(result, new int[] { 197, 199, 200, 201, 203 });
55   }
56 
57   /**
58    * Run CLARA with fixed parameters and compare the result to a golden
59    * standard.
60    */
61   @Test
testSingleAssignmentKMeansPlusPlusMedoids()62   public void testSingleAssignmentKMeansPlusPlusMedoids() {
63     Database db = makeSimpleDatabase(UNITTEST + "different-densities-2d-no-noise.ascii", 1000);
64     Clustering<?> result = new ELKIBuilder<CLARA<DoubleVector>>(CLARA.class) //
65         .with(KMeans.K_ID, 5) //
66         .with(KMeans.SEED_ID, 3) //
67         .with(KMeans.INIT_ID, KMeansPlusPlusInitialMeans.class) //
68         .with(KMeans.MAXITER_ID, 1) //
69         .with(CLARA.Parameterizer.NOKEEPMED_ID) //
70         .with(CLARA.Parameterizer.SAMPLESIZE_ID, 10) //
71         .with(CLARA.Parameterizer.RANDOM_ID, 0) //
72         .build().run(db);
73     testFMeasure(db, result, 0.99602);
74     testClusterSizes(result, new int[] { 198, 200, 200, 200, 202 });
75   }
76 }
77