MultiKMeansPlusPlusClusterer.java

  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. package org.apache.commons.math4.legacy.ml.clustering;

  18. import java.util.Collection;
  19. import java.util.List;

  20. import org.apache.commons.math4.legacy.ml.clustering.evaluation.SumOfClusterVariances;

  21. /**
  22.  * A wrapper around a k-means++ clustering algorithm which performs multiple trials
  23.  * and returns the best solution.
  24.  * @param <T> type of the points to cluster
  25.  * @since 3.2
  26.  */
  27. public class MultiKMeansPlusPlusClusterer<T extends Clusterable> extends Clusterer<T> {

  28.     /** The underlying k-means clusterer. */
  29.     private final KMeansPlusPlusClusterer<T> clusterer;

  30.     /** The number of trial runs. */
  31.     private final int numTrials;

  32.     /** The cluster evaluator to use. */
  33.     private final ClusterRanking evaluator;

  34.     /** Build a clusterer.
  35.      * @param clusterer the k-means clusterer to use
  36.      * @param numTrials number of trial runs
  37.      */
  38.     public MultiKMeansPlusPlusClusterer(final KMeansPlusPlusClusterer<T> clusterer,
  39.                                         final int numTrials) {
  40.         this(clusterer,
  41.              numTrials,
  42.              ClusterEvaluator.ranking(new SumOfClusterVariances(clusterer.getDistanceMeasure())));
  43.     }

  44.     /** Build a clusterer.
  45.      * @param clusterer the k-means clusterer to use
  46.      * @param numTrials number of trial runs
  47.      * @param evaluator the cluster evaluator to use
  48.      * @since 3.3
  49.      */
  50.     public MultiKMeansPlusPlusClusterer(final KMeansPlusPlusClusterer<T> clusterer,
  51.                                         final int numTrials,
  52.                                         final ClusterRanking evaluator) {
  53.         super(clusterer.getDistanceMeasure());
  54.         this.clusterer = clusterer;
  55.         this.numTrials = numTrials;
  56.         this.evaluator = evaluator;
  57.     }

  58.     /**
  59.      * Runs the K-means++ clustering algorithm.
  60.      *
  61.      * @param points the points to cluster
  62.      * @return a list of clusters containing the points
  63.      * @throws org.apache.commons.math4.legacy.exception.MathIllegalArgumentException if
  64.      * the data points are null or the number of clusters is larger than the
  65.      * number of data points
  66.      * @throws org.apache.commons.math4.legacy.exception.ConvergenceException if
  67.      * an empty cluster is encountered and the underlying {@link KMeansPlusPlusClusterer}
  68.      * has its {@link KMeansPlusPlusClusterer.EmptyClusterStrategy} is set to {@code ERROR}.
  69.      */
  70.     @Override
  71.     public List<CentroidCluster<T>> cluster(final Collection<T> points) {
  72.         // at first, we have not found any clusters list yet
  73.         List<CentroidCluster<T>> best = null;
  74.         double bestRank = Double.NEGATIVE_INFINITY;

  75.         // do several clustering trials
  76.         for (int i = 0; i < numTrials; ++i) {

  77.             // compute a clusters list
  78.             List<CentroidCluster<T>> clusters = clusterer.cluster(points);

  79.             // compute the rank of the current list
  80.             final double rank = evaluator.compute(clusters);

  81.             if (rank > bestRank) {
  82.                 // this one is the best we have found so far, remember it
  83.                 best = clusters;
  84.                 bestRank = rank;
  85.             }
  86.         }

  87.         // return the best clusters list found
  88.         return best;
  89.     }
  90. }