Top Description Inners Fields Constructors Methods
org.apache.commons.math3.ml.clustering

public Class KMeansPlusPlusClusterer<T extends Clusterable>

extends Clusterer<T extends Clusterable>
Class Inheritance
Type Parameters
<T>
type of the points to cluster
Imports
java.util.ArrayList, .Collection, .Collections, .List, org.apache.commons.math3.exception.ConvergenceException, .MathIllegalArgumentException, .NumberIsTooSmallException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.ml.distance.DistanceMeasure, .EuclideanDistance, org.apache.commons.math3.random.JDKRandomGenerator, .RandomGenerator, org.apache.commons.math3.stat.descriptive.moment.Variance, org.apache.commons.math3.util.MathUtils

Clustering algorithm based on David Arthur and Sergei Vassilvitski k-means++ algorithm.
Since
3.2
See Also
K-means++ (wikipedia)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
KMeansPlusPlusClusterer.EmptyClusterStrategy

Strategies to use for replacing an empty cluster.

Field Summary

Modifier and TypeField and Description
private final KMeansPlusPlusClusterer.EmptyClusterStrategy
emptyStrategy

Selected strategy for empty clusters.

private final int
k

The number of clusters.

private final int
maxIterations

The maximum number of iterations.

private final RandomGenerator
random

Random generator for choosing initial centers.

Constructor Summary

AccessConstructor and Description
public
KMeansPlusPlusClusterer(final int
the number of clusters to split the data into
k
)

Build a clusterer.

public
KMeansPlusPlusClusterer(final int
the number of clusters to split the data into
k
,
final int
the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.
maxIterations
)

Build a clusterer.

public
KMeansPlusPlusClusterer(final int
the number of clusters to split the data into
k
,
final int
the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.
maxIterations
,
final DistanceMeasure
the distance measure to use
measure
)

Build a clusterer.

public
KMeansPlusPlusClusterer(final int
the number of clusters to split the data into
k
,
final int
the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.
maxIterations
,
final DistanceMeasure
the distance measure to use
measure
,
final RandomGenerator
random generator to use for choosing initial centers
random
)

Build a clusterer.

public
KMeansPlusPlusClusterer(final int
the number of clusters to split the data into
k
,
final int
the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.
maxIterations
,
final DistanceMeasure
the distance measure to use
measure
,
final RandomGenerator
random generator to use for choosing initial centers
random
,
final KMeansPlusPlusClusterer.EmptyClusterStrategy
strategy to use for handling empty clusters that may appear during algorithm iterations
emptyStrategy
)

Build a clusterer.

Method Summary

Modifier and TypeMethod and Description
private int

Returns:

the number of points assigned to different clusters as the iteration before
assignPointsToClusters
(final List<CentroidCluster<T>>
the Clusters to add the points to
clusters
,
final Collection<T>
the points to add to the given Clusters
points
,
final int[]
points assignments to clusters
assignments
)

Adds the given points to the closest Cluster.

private Clusterable

Returns:

the computed centroid for the set of points
centroidOf
(final Collection<T>
the set of points
points
,
final int
the point dimension
dimension
)

Computes the centroid for a set of points.

private List<CentroidCluster<T>>

Returns:

the initial centers
chooseInitialCenters
(final Collection<T>
the points to choose the initial centers from
points
)

Use K-means++ to choose the initial centers.

public List<CentroidCluster<T>>

Returns:

a list of clusters containing the points
cluster
(final Collection<T>
the points to cluster
points
)

Implements abstract org.apache.commons.math3.ml.clustering.Clusterer.cluster.

Runs the K-means++ clustering algorithm.

public KMeansPlusPlusClusterer.EmptyClusterStrategy
getEmptyClusterStrategy()

Returns the EmptyClusterStrategy used by this instance.

private T

Returns:

point farthest to its cluster center
getFarthestPoint
(final Collection<CentroidCluster<T>>
the Clusters to search
clusters
)

Get the point farthest to its cluster center

public int

Returns:

the number of clusters
getK
()

Return the number of clusters this instance will use.

public int

Returns:

the maximum number of iterations, or -1 if no maximum is set
getMaxIterations
()

Returns the maximum number of iterations this instance will use.

private int

Returns:

the index of the nearest Cluster to the given point
getNearestCluster
(final Collection<CentroidCluster<T>>
the Clusters to search
clusters
,
final T
the point to find the nearest Cluster for
point
)

Returns the nearest Cluster to the given point

private T

Returns:

a random point from the selected cluster
getPointFromLargestNumberCluster
(final Collection<? extends Cluster<T>>
the Clusters to search
clusters
)

Get a random point from the Cluster with the largest number of points

private T

Returns:

a random point from the selected cluster
getPointFromLargestVarianceCluster
(final Collection<CentroidCluster<T>>
the Clusters to search
clusters
)

Get a random point from the Cluster with the largest distance variance.

public RandomGenerator

Returns:

the random generator
getRandomGenerator
()

Returns the random generator this instance will use.

Inherited from org.apache.commons.math3.ml.clustering.Clusterer:
distancegetDistanceMeasure

Field Detail

emptyStrategyback to summary
private final KMeansPlusPlusClusterer.EmptyClusterStrategy emptyStrategy

Selected strategy for empty clusters.

kback to summary
private final int k

The number of clusters.

maxIterationsback to summary
private final int maxIterations

The maximum number of iterations.

randomback to summary
private final RandomGenerator random

Random generator for choosing initial centers.

Constructor Detail

KMeansPlusPlusClustererback to summary
public KMeansPlusPlusClusterer(final 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:int

the number of clusters to split the data into

KMeansPlusPlusClustererback to summary
public KMeansPlusPlusClusterer(final int k, final 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:int

the number of clusters to split the data into

maxIterations:int

the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.

KMeansPlusPlusClustererback to summary
public KMeansPlusPlusClusterer(final int k, final int maxIterations, final DistanceMeasure measure)

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:int

the number of clusters to split the data into

maxIterations:int

the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.

measure:DistanceMeasure

the distance measure to use

KMeansPlusPlusClustererback to summary
public KMeansPlusPlusClusterer(final int k, final int maxIterations, final DistanceMeasure measure, final 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:int

the number of clusters to split the data into

maxIterations:int

the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.

measure:DistanceMeasure

the distance measure to use

random:RandomGenerator

random generator to use for choosing initial centers

KMeansPlusPlusClustererback to summary
public KMeansPlusPlusClusterer(final int k, final int maxIterations, final DistanceMeasure measure, final RandomGenerator random, final KMeansPlusPlusClusterer.EmptyClusterStrategy emptyStrategy)

Build a clusterer.

Parameters
k:int

the number of clusters to split the data into

maxIterations:int

the maximum number of iterations to run the algorithm for. If negative, no maximum will be used.

measure:DistanceMeasure

the distance measure to use

random:RandomGenerator

random generator to use for choosing initial centers

emptyStrategy:KMeansPlusPlusClusterer.EmptyClusterStrategy

strategy to use for handling empty clusters that may appear during algorithm iterations

Method Detail

assignPointsToClustersback to summary
private int assignPointsToClusters(final List<CentroidCluster<T>> clusters, final Collection<T> points, final int[] assignments)

Adds the given points to the closest Cluster.

Parameters
clusters:List<CentroidCluster<T>>

the Clusters to add the points to

points:Collection<T>

the points to add to the given Clusters

assignments:int[]

points assignments to clusters

Returns:int

the number of points assigned to different clusters as the iteration before

centroidOfback to summary
private Clusterable centroidOf(final Collection<T> points, final int dimension)

Computes the centroid for a set of points.

Parameters
points:Collection<T>

the set of points

dimension:int

the point dimension

Returns:Clusterable

the computed centroid for the set of points

chooseInitialCentersback to summary
private List<CentroidCluster<T>> chooseInitialCenters(final Collection<T> points)

Use K-means++ to choose the initial centers.

Parameters
points:Collection<T>

the points to choose the initial centers from

Returns:List<CentroidCluster<T>>

the initial centers

clusterback to summary
public List<CentroidCluster<T>> cluster(final Collection<T> points) throws MathIllegalArgumentException, ConvergenceException

Implements abstract org.apache.commons.math3.ml.clustering.Clusterer.cluster.

Runs the K-means++ clustering algorithm.

Parameters
points:Collection<T>

the points to cluster

Returns:List<CentroidCluster<T>>

a list of clusters containing the points

Annotations
@Override
Exceptions
MathIllegalArgumentException:
if the data points are null or the number of clusters is larger than the number of data points
ConvergenceException:
if an empty cluster is encountered and the emptyStrategy is set to ERROR
getEmptyClusterStrategyback to summary
public KMeansPlusPlusClusterer.EmptyClusterStrategy getEmptyClusterStrategy()

Returns the EmptyClusterStrategy used by this instance.

Returns:KMeansPlusPlusClusterer.EmptyClusterStrategy

the EmptyClusterStrategy

getFarthestPointback to summary
private T getFarthestPoint(final Collection<CentroidCluster<T>> clusters) throws ConvergenceException

Get the point farthest to its cluster center

Parameters
clusters:Collection<CentroidCluster<T>>

the Clusters to search

Returns:T

point farthest to its cluster center

Exceptions
ConvergenceException:
if clusters are all empty
getKback to summary
public int getK()

Return the number of clusters this instance will use.

Returns:int

the number of clusters

getMaxIterationsback to summary
public int getMaxIterations()

Returns the maximum number of iterations this instance will use.

Returns:int

the maximum number of iterations, or -1 if no maximum is set

getNearestClusterback to summary
private int getNearestCluster(final Collection<CentroidCluster<T>> clusters, final T point)

Returns the nearest Cluster to the given point

Parameters
clusters:Collection<CentroidCluster<T>>

the Clusters to search

point:T

the point to find the nearest Cluster for

Returns:int

the index of the nearest Cluster to the given point

getPointFromLargestNumberClusterback to summary
private T getPointFromLargestNumberCluster(final Collection<? extends Cluster<T>> clusters) throws ConvergenceException

Get a random point from the Cluster with the largest number of points

Parameters
clusters:Collection<? extends Cluster<T>>

the Clusters to search

Returns:T

a random point from the selected cluster

Exceptions
ConvergenceException:
if clusters are all empty
getPointFromLargestVarianceClusterback to summary
private T getPointFromLargestVarianceCluster(final Collection<CentroidCluster<T>> clusters) throws ConvergenceException

Get a random point from the Cluster with the largest distance variance.

Parameters
clusters:Collection<CentroidCluster<T>>

the Clusters to search

Returns:T

a random point from the selected cluster

Exceptions
ConvergenceException:
if clusters are all empty
getRandomGeneratorback to summary
public RandomGenerator getRandomGenerator()

Returns the random generator this instance will use.

Returns:RandomGenerator

the random generator

org.apache.commons.math3.ml.clustering back to summary

public final Enum KMeansPlusPlusClusterer.EmptyClusterStrategy

extends Enum<KMeansPlusPlusClusterer.EmptyClusterStrategy>
Class Inheritance

Strategies to use for replacing an empty cluster.

Field Summary

Modifier and TypeField and Description
public static final KMeansPlusPlusClusterer.EmptyClusterStrategy
ERROR

Generate an error.

public static final KMeansPlusPlusClusterer.EmptyClusterStrategy
FARTHEST_POINT

Create a cluster around the point farthest from its centroid.

public static final KMeansPlusPlusClusterer.EmptyClusterStrategy
LARGEST_POINTS_NUMBER

Split the cluster with largest number of points.

public static final KMeansPlusPlusClusterer.EmptyClusterStrategy
LARGEST_VARIANCE

Split the cluster with largest distance variance.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static KMeansPlusPlusClusterer.EmptyClusterStrategy
public static KMeansPlusPlusClusterer.EmptyClusterStrategy[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ERRORback to summary
public static final KMeansPlusPlusClusterer.EmptyClusterStrategy ERROR

Generate an error.

FARTHEST_POINTback to summary
public static final KMeansPlusPlusClusterer.EmptyClusterStrategy FARTHEST_POINT

Create a cluster around the point farthest from its centroid.

LARGEST_POINTS_NUMBERback to summary
public static final KMeansPlusPlusClusterer.EmptyClusterStrategy LARGEST_POINTS_NUMBER

Split the cluster with largest number of points.

LARGEST_VARIANCEback to summary
public static final KMeansPlusPlusClusterer.EmptyClusterStrategy LARGEST_VARIANCE

Split the cluster with largest distance variance.

Constructor Detail

EmptyClusterStrategyback to summary
private EmptyClusterStrategy()

Method Detail

valueOfback to summary
public static KMeansPlusPlusClusterer.EmptyClusterStrategy valueOf(String name)
valuesback to summary
public static KMeansPlusPlusClusterer.EmptyClusterStrategy[] values()