Class KMeansPlusPlusClusterer<T extends Clusterable>
java.lang.Object
org.apache.commons.math3.ml.clustering.Clusterer<T>
org.apache.commons.math3.ml.clustering.KMeansPlusPlusClusterer<T>
- Type Parameters:
T- type of the points to cluster
Clustering algorithm based on David Arthur and Sergei Vassilvitski k-means++ algorithm.
- Since:
- 3.2
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumStrategies to use for replacing an empty cluster. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final KMeansPlusPlusClusterer.EmptyClusterStrategySelected strategy for empty clusters.private final intThe number of clusters.private final intThe maximum number of iterations.private final RandomGeneratorRandom generator for choosing initial centers. -
Constructor Summary
ConstructorsConstructorDescriptionKMeansPlusPlusClusterer(int k) Build a clusterer.KMeansPlusPlusClusterer(int k, int maxIterations) Build a clusterer.KMeansPlusPlusClusterer(int k, int maxIterations, DistanceMeasure measure) Build a clusterer.KMeansPlusPlusClusterer(int k, int maxIterations, DistanceMeasure measure, RandomGenerator random) Build a clusterer.KMeansPlusPlusClusterer(int k, int maxIterations, DistanceMeasure measure, RandomGenerator random, KMeansPlusPlusClusterer.EmptyClusterStrategy emptyStrategy) Build a clusterer. -
Method Summary
Modifier and TypeMethodDescriptionprivate intassignPointsToClusters(List<CentroidCluster<T>> clusters, Collection<T> points, int[] assignments) Adds the given points to the closestCluster.private ClusterablecentroidOf(Collection<T> points, int dimension) Computes the centroid for a set of points.private List<CentroidCluster<T>> chooseInitialCenters(Collection<T> points) Use K-means++ to choose the initial centers.cluster(Collection<T> points) Runs the K-means++ clustering algorithm.Returns theKMeansPlusPlusClusterer.EmptyClusterStrategyused by this instance.private TgetFarthestPoint(Collection<CentroidCluster<T>> clusters) Get the point farthest to its cluster centerintgetK()Return the number of clusters this instance will use.intReturns the maximum number of iterations this instance will use.private intgetNearestCluster(Collection<CentroidCluster<T>> clusters, T point) Returns the nearestClusterto the given pointprivate TgetPointFromLargestNumberCluster(Collection<? extends Cluster<T>> clusters) Get a random point from theClusterwith the largest number of pointsprivate TgetPointFromLargestVarianceCluster(Collection<CentroidCluster<T>> clusters) Get a random point from theClusterwith the largest distance variance.Returns the random generator this instance will use.Methods inherited from class org.apache.commons.math3.ml.clustering.Clusterer
distance, getDistanceMeasure
-
Field Details
-
k
private final int kThe number of clusters. -
maxIterations
private final int maxIterationsThe maximum number of iterations. -
random
Random generator for choosing initial centers. -
emptyStrategy
Selected strategy for empty clusters.
-
-
Constructor Details
-
KMeansPlusPlusClusterer
public KMeansPlusPlusClusterer(int k) Build a clusterer.The default strategy for handling empty clusters that may appear during algorithm iterations is to split the cluster with largest distance variance.
The euclidean distance will be used as default distance measure.
- Parameters:
k- the number of clusters to split the data into
-
KMeansPlusPlusClusterer
public KMeansPlusPlusClusterer(int k, int maxIterations) Build a clusterer.The default strategy for handling empty clusters that may appear during algorithm iterations is to split the cluster with largest distance variance.
The euclidean distance will be used as default distance measure.
- Parameters:
k- the number of clusters to split the data intomaxIterations- the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.
-
KMeansPlusPlusClusterer
Build a clusterer.The default strategy for handling empty clusters that may appear during algorithm iterations is to split the cluster with largest distance variance.
- Parameters:
k- the number of clusters to split the data intomaxIterations- the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.measure- the distance measure to use
-
KMeansPlusPlusClusterer
public KMeansPlusPlusClusterer(int k, int maxIterations, DistanceMeasure measure, RandomGenerator random) Build a clusterer.The default strategy for handling empty clusters that may appear during algorithm iterations is to split the cluster with largest distance variance.
- Parameters:
k- the number of clusters to split the data intomaxIterations- the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.measure- the distance measure to userandom- random generator to use for choosing initial centers
-
KMeansPlusPlusClusterer
public KMeansPlusPlusClusterer(int k, int maxIterations, DistanceMeasure measure, RandomGenerator random, KMeansPlusPlusClusterer.EmptyClusterStrategy emptyStrategy) Build a clusterer.- Parameters:
k- the number of clusters to split the data intomaxIterations- the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.measure- the distance measure to userandom- random generator to use for choosing initial centersemptyStrategy- strategy to use for handling empty clusters that may appear during algorithm iterations
-
-
Method Details
-
getK
public int getK()Return the number of clusters this instance will use.- Returns:
- the number of clusters
-
getMaxIterations
public int getMaxIterations()Returns the maximum number of iterations this instance will use.- Returns:
- the maximum number of iterations, or -1 if no maximum is set
-
getRandomGenerator
Returns the random generator this instance will use.- Returns:
- the random generator
-
getEmptyClusterStrategy
Returns theKMeansPlusPlusClusterer.EmptyClusterStrategyused by this instance.- Returns:
- the
KMeansPlusPlusClusterer.EmptyClusterStrategy
-
cluster
public List<CentroidCluster<T>> cluster(Collection<T> points) throws MathIllegalArgumentException, ConvergenceException Runs the K-means++ clustering algorithm.- Specified by:
clusterin classClusterer<T extends Clusterable>- Parameters:
points- the points to cluster- Returns:
- a list of clusters containing the points
- Throws:
MathIllegalArgumentException- if the data points are null or the number of clusters is larger than the number of data pointsConvergenceException- if an empty cluster is encountered and theemptyStrategyis set toERROR
-
assignPointsToClusters
private int assignPointsToClusters(List<CentroidCluster<T>> clusters, Collection<T> points, int[] assignments) Adds the given points to the closestCluster. -
chooseInitialCenters
Use K-means++ to choose the initial centers.- Parameters:
points- the points to choose the initial centers from- Returns:
- the initial centers
-
getPointFromLargestVarianceCluster
private T getPointFromLargestVarianceCluster(Collection<CentroidCluster<T>> clusters) throws ConvergenceException Get a random point from theClusterwith the largest distance variance.- Parameters:
clusters- theClusters to search- Returns:
- a random point from the selected cluster
- Throws:
ConvergenceException- if clusters are all empty
-
getPointFromLargestNumberCluster
private T getPointFromLargestNumberCluster(Collection<? extends Cluster<T>> clusters) throws ConvergenceException Get a random point from theClusterwith the largest number of points- Parameters:
clusters- theClusters to search- Returns:
- a random point from the selected cluster
- Throws:
ConvergenceException- if clusters are all empty
-
getFarthestPoint
Get the point farthest to its cluster center- Parameters:
clusters- theClusters to search- Returns:
- point farthest to its cluster center
- Throws:
ConvergenceException- if clusters are all empty
-
getNearestCluster
Returns the nearestClusterto the given point -
centroidOf
Computes the centroid for a set of points.- Parameters:
points- the set of pointsdimension- the point dimension- Returns:
- the computed centroid for the set of points
-