Package edu.uci.ics.jung.algorithms.util
Class KMeansClusterer<T>
- java.lang.Object
-
- edu.uci.ics.jung.algorithms.util.KMeansClusterer<T>
-
public class KMeansClusterer<T> extends java.lang.ObjectGroups items into a specified number of clusters, based on their proximity in d-dimensional space, using the k-means algorithm. Calls toclusterwill terminate when either of the two following conditions is true:-
the number of iterations is >
max_iterationsnone of the centroids has moved as much asconvergence_thresholdsince the previous iteration
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKMeansClusterer.NotEnoughClustersExceptionAn exception that indicates that the specified data points cannot be clustered into the number of clusters requested by the user.
-
Field Summary
Fields Modifier and Type Field Description protected doubleconvergence_thresholdprotected intmax_iterationsprotected java.util.Randomrand
-
Constructor Summary
Constructors Constructor Description KMeansClusterer()Creates an instance with max iterations of 100 and convergence threshold of 0.001.KMeansClusterer(int max_iterations, double convergence_threshold)Creates an instance whose termination conditions are set according to the parameters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.Map<double[],java.util.Map<T,double[]>>assignToClusters(java.util.Map<T,double[]> object_locations, java.util.Set<double[]> centroids)Assigns each object to the cluster whose centroid is closest to the object.java.util.Collection<java.util.Map<T,double[]>>cluster(java.util.Map<T,double[]> object_locations, int num_clusters)Returns aCollectionof clusters, where each cluster is represented as aMapofObjectsto locations in d-dimensional space.doublegetConvergenceThreshold()Returns the convergence threshold.intgetMaxIterations()Returns the maximum number of iterations.voidsetConvergenceThreshold(double convergence_threshold)Sets the convergence threshold.voidsetMaxIterations(int max_iterations)Sets the maximum number of iterations.voidsetSeed(int random_seed)Sets the seed used by the internal random number generator.
-
-
-
Constructor Detail
-
KMeansClusterer
public KMeansClusterer(int max_iterations, double convergence_threshold)Creates an instance whose termination conditions are set according to the parameters.
-
KMeansClusterer
public KMeansClusterer()
Creates an instance with max iterations of 100 and convergence threshold of 0.001.
-
-
Method Detail
-
getMaxIterations
public int getMaxIterations()
Returns the maximum number of iterations.
-
setMaxIterations
public void setMaxIterations(int max_iterations)
Sets the maximum number of iterations.
-
getConvergenceThreshold
public double getConvergenceThreshold()
Returns the convergence threshold.
-
setConvergenceThreshold
public void setConvergenceThreshold(double convergence_threshold)
Sets the convergence threshold.- Parameters:
convergence_threshold-
-
cluster
public java.util.Collection<java.util.Map<T,double[]>> cluster(java.util.Map<T,double[]> object_locations, int num_clusters)
Returns aCollectionof clusters, where each cluster is represented as aMapofObjectsto locations in d-dimensional space.- Parameters:
object_locations- a map of the Objects to cluster, todoublearrays that specify their locations in d-dimensional space.num_clusters- the number of clusters to create- Throws:
KMeansClusterer.NotEnoughClustersException
-
assignToClusters
protected java.util.Map<double[],java.util.Map<T,double[]>> assignToClusters(java.util.Map<T,double[]> object_locations, java.util.Set<double[]> centroids)
Assigns each object to the cluster whose centroid is closest to the object.- Parameters:
object_locations- a map of objects to locationscentroids- the centroids of the clusters to be formed- Returns:
- a map of objects to assigned clusters
-
setSeed
public void setSeed(int random_seed)
Sets the seed used by the internal random number generator. Enables consistent outputs.
-
-