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

public Class KMeansPlusPlusClusterer<T extends Clusterable<T>>

extends Object
Class Inheritance
Annotations
@Deprecated
Type Parameters
<T>
type of the points to cluster
Imports
java.util.ArrayList, .Collection, .Collections, .List, .Random, org.apache.commons.math3.exception.ConvergenceException, .MathIllegalArgumentException, .NumberIsTooSmallException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.stat.descriptive.moment.Variance, org.apache.commons.math3.util.MathUtils

Deprecated

As of 3.2 (to be removed in 4.0), use org.apache.commons.math3.ml.clustering.KMeansPlusPlusClusterer instead

Clustering algorithm based on David Arthur and Sergei Vassilvitski k-means++ algorithm.
Since
2.0
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 Random
random

Random generator for choosing initial centers.

Constructor Summary

AccessConstructor and Description
public
KMeansPlusPlusClusterer(final Random
random generator to use for choosing initial centers
random
)

Build a clusterer.

public
KMeansPlusPlusClusterer(final Random
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 static <
type of the points to cluster
T extends Clusterable<T>
>
int

Returns:

the number of points assigned to different clusters as the iteration before
assignPointsToClusters
(final List<Cluster<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 static <
type of the points to cluster
T extends Clusterable<T>
>
List<Cluster<T>>

Returns:

the initial centers
chooseInitialCenters
(final Collection<T>
the points to choose the initial centers from
points
,
final int
the number of centers to choose
k
,
final Random
random generator to use
random
)

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

public List<Cluster<T>>

Returns:

a list of clusters containing the points
cluster
(final Collection<T>
the points to cluster
points
,
final int
the number of clusters to split the data into
k
,
int
number of trial runs
numTrials
,
int
the maximum number of iterations to run the algorithm for at each trial run. If negative, no maximum will be used
maxIterationsPerTrial
)

Runs the K-means++ clustering algorithm.

public List<Cluster<T>>

Returns:

a list of clusters containing the points
cluster
(final Collection<T>
the points to cluster
points
,
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
)

Runs the K-means++ clustering algorithm.

private T

Returns:

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

Get the point farthest to its cluster center

private static <
type of the points to cluster
T extends Clusterable<T>
>
int

Returns:

the index of the nearest Cluster to the given point
getNearestCluster
(final Collection<Cluster<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<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<Cluster<T>>
the Clusters to search
clusters
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

emptyStrategyback to summary
private final KMeansPlusPlusClusterer.EmptyClusterStrategy emptyStrategy

Selected strategy for empty clusters.

randomback to summary
private final Random random

Random generator for choosing initial centers.

Constructor Detail

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

random generator to use for choosing initial centers

KMeansPlusPlusClustererback to summary
public KMeansPlusPlusClusterer(final Random random, final KMeansPlusPlusClusterer.EmptyClusterStrategy emptyStrategy)

Build a clusterer.

Parameters
random:Random

random generator to use for choosing initial centers

emptyStrategy:KMeansPlusPlusClusterer.EmptyClusterStrategy

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

Since
2.2

Method Detail

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

Adds the given points to the closest Cluster.

Parameters
<T>
type of the points to cluster
clusters:List<Cluster<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

chooseInitialCentersback to summary
private static <T extends Clusterable<T>> List<Cluster<T>> chooseInitialCenters(final Collection<T> points, final int k, final Random random)

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

Parameters
<T>
type of the points to cluster
points:Collection<T>

the points to choose the initial centers from

k:int

the number of centers to choose

random:Random

random generator to use

Returns:List<Cluster<T>>

the initial centers

clusterback to summary
public List<Cluster<T>> cluster(final Collection<T> points, final int k, int numTrials, int maxIterationsPerTrial) throws MathIllegalArgumentException, ConvergenceException

Runs the K-means++ clustering algorithm.

Parameters
points:Collection<T>

the points to cluster

k:int

the number of clusters to split the data into

numTrials:int

number of trial runs

maxIterationsPerTrial:int

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

Returns:List<Cluster<T>>

a list of clusters containing the points

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
clusterback to summary
public List<Cluster<T>> cluster(final Collection<T> points, final int k, final int maxIterations) throws MathIllegalArgumentException, ConvergenceException

Runs the K-means++ clustering algorithm.

Parameters
points:Collection<T>

the points to cluster

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

Returns:List<Cluster<T>>

a list of clusters containing the points

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
getFarthestPointback to summary
private T getFarthestPoint(final Collection<Cluster<T>> clusters) throws ConvergenceException

Get the point farthest to its cluster center

Parameters
clusters:Collection<Cluster<T>>

the Clusters to search

Returns:T

point farthest to its cluster center

Exceptions
ConvergenceException:
if clusters are all empty
getNearestClusterback to summary
private static <T extends Clusterable<T>> int getNearestCluster(final Collection<Cluster<T>> clusters, final T point)

Returns the nearest Cluster to the given point

Parameters
<T>
type of the points to cluster
clusters:Collection<Cluster<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<Cluster<T>> clusters) throws ConvergenceException

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

Parameters
clusters:Collection<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<Cluster<T>> clusters) throws ConvergenceException

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

Parameters
clusters:Collection<Cluster<T>>

the Clusters to search

Returns:T

a random point from the selected cluster

Exceptions
ConvergenceException:
if clusters are all empty
org.apache.commons.math3.stat.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()