Package edu.cmu.tetrad.cluster
Class KMeans
java.lang.Object
edu.cmu.tetrad.cluster.KMeans
- All Implemented Interfaces:
ClusteringAlgorithm
Implements the "batch" version of the K Means clustering algorithm-- that is, in one sweep, assign each point to its
nearest center, and then in a second sweep, reset each center to the mean of the cluster for that center, repeating
until convergence.
Note that this algorithm is guaranteed to converge, since the total squared error is guaranteed to be reduced at each step.
- Author:
- josephramsey
-
Method Summary
Modifier and TypeMethodDescriptionvoidRuns the batch K-means clustering algorithm on the data, returning a result.getCluster(int k) intReturn the maximum number of iterations, or -1 if the algorithm is allowed to run unconstrainted.intbooleanintstatic KMeansrandomClusters(int numCenters) Constructs a new KMeansBatch, initializing the algorithm by randomly assigning each point in the data to one of the numCenters clusters, then calculating the centroid of each cluster.static KMeansrandomPoints(int numCenters) Constructs a new KMeansBatch, initializing the algorithm by pickingnumCeneterscenters randomly from the data itself.voidsetMaxIterations(int maxIterations) Sets the maximum number of iterations, or -1 if the algorithm is allowed to run unconstrainted.voidsetVerbose(boolean verbose) True iff verbose output should be printed.toString()
-
Method Details
-
randomPoints
Constructs a new KMeansBatch, initializing the algorithm by pickingnumCeneterscenters randomly from the data itself.- Parameters:
numCenters- The number of centers (clusters).- Returns:
- The parametrized algorithm.
-
randomClusters
Constructs a new KMeansBatch, initializing the algorithm by randomly assigning each point in the data to one of the numCenters clusters, then calculating the centroid of each cluster.- Parameters:
numCenters- The number of centers (clusters).- Returns:
- The constructed algorithm.
-
cluster
Runs the batch K-means clustering algorithm on the data, returning a result.- Specified by:
clusterin interfaceClusteringAlgorithm- Parameters:
data- An n x m double matrix with n cases (rows) and m variables (columns). Makes an int array c such that c[i] is the cluster that case i is placed into, or -1 if case i is not placed into a cluster (as a result of its being eliminated from consideration, for instance).
-
getClusters
- Specified by:
getClustersin interfaceClusteringAlgorithm- Returns:
- a list of clusters, each consisting of a list of indices in the dataset provided as an argument to
cluster, or null if the data has not yet been clustered.
-
getMaxIterations
public int getMaxIterations()Return the maximum number of iterations, or -1 if the algorithm is allowed to run unconstrainted.- Returns:
- This value.
-
setMaxIterations
public void setMaxIterations(int maxIterations) Sets the maximum number of iterations, or -1 if the algorithm is allowed to run unconstrainted.- Parameters:
maxIterations- This value.
-
getNumClusters
public int getNumClusters() -
getCluster
-
iterations
public int iterations()- Returns:
- the number of iterations.
-
toString
-
isVerbose
public boolean isVerbose() -
setVerbose
public void setVerbose(boolean verbose) Description copied from interface:ClusteringAlgorithmTrue iff verbose output should be printed.- Specified by:
setVerbosein interfaceClusteringAlgorithm
-