Catalano.MachineLearning.Clustering
Class KMeans
- java.lang.Object
-
- Catalano.MachineLearning.Clustering.PartitionClustering<double[]>
-
- Catalano.MachineLearning.Clustering.KMeans
-
- All Implemented Interfaces:
- ICentroidClustering, java.io.Serializable
public class KMeans extends PartitionClustering<double[]> implements ICentroidClustering
K-Means learn aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest mean. Although finding an exact solution to the k-means problem for arbitrary input is NP-hard, the standard approach to finding an approximate solution (often called Lloyd's algorithm or the k-means algorithm) is used widely and frequently finds reasonable solutions quickly.However, the k-means algorithm has at least two major theoretic shortcomings:
- First, it has been shown that the worst case running time of the algorithm is super-polynomial in the input size.
- Second, the approximation found can be arbitrarily bad with respect to the objective function compared to the optimal learn.
We also use k-d trees to speed up each k-means step as described in the filter algorithm by Kanungo, et al.
K-means is a hard clustering method, i.e. each sample is assigned to a specific cluster. In contrast, soft clustering, e.g. the Expectation-Maximization algorithm for Gaussian mixtures, assign samples to different clusters with different probabilities.
References
- Tapas Kanungo, David M. Mount, Nathan S. Netanyahu, Christine D. Piatko, Ruth Silverman, and Angela Y. Wu. An Efficient k-Means Clustering Algorithm: Analysis and Implementation. IEEE TRANS. PAMI, 2002.
- D. Arthur and S. Vassilvitskii. "K-means++: the advantages of careful seeding". ACM-SIAM symposium on Discrete algorithms, 1027-1035, 2007.
- Anna D. Peterson, Arka P. Ghosh and Ranjan Maitra. A systematic evaluation of different methods for initializing the K-means clustering algorithm. 2010.
- See Also:
BBDTree, Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description KMeans()Initializes a new instance of the KMeans class.KMeans(int k)Initializes a new instance of the KMeans class.KMeans(int k, int maxIteration)Initializes a new instance of the KMeans class.KMeans(int k, int maxIteration, int maxRuns)Initializes a new instance of the KMeans class.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidCompute(DatasetClassification dataset)Compute the data.voidCompute(double[][] input)Compute the data.doubledistortion()Returns the distortion.double[][]getCentroids()Returns the centroids.intgetClusters()static KMeanslloyd(double[][] input, int k)The implementation of Lloyd algorithm as a benchmark.static KMeanslloyd(double[][] input, int k, int maxIteration)The implementation of Lloyd algorithm as a benchmark.static KMeanslloyd(double[][] input, int k, int maxIteration, int runs)The implementation of Lloyd algorithm as a benchmark.intPredict(double[] x)Cluster a new instance.voidsetCluster(int k)java.lang.StringtoString()-
Methods inherited from class Catalano.MachineLearning.Clustering.PartitionClustering
getClusterLabel, getClusterSize, getNumClusters
-
-
-
-
Constructor Detail
-
KMeans
public KMeans()
Initializes a new instance of the KMeans class.
-
KMeans
public KMeans(int k)
Initializes a new instance of the KMeans class.- Parameters:
k- Number of the clusters.
-
KMeans
public KMeans(int k, int maxIteration)Initializes a new instance of the KMeans class.- Parameters:
k- Number of the clusters.maxIteration- Maximum iteration.
-
KMeans
public KMeans(int k, int maxIteration, int maxRuns)Initializes a new instance of the KMeans class.- Parameters:
k- Number of the clusters.maxIteration- Maximum iteration.maxRuns- Perform KMeans n times.
-
-
Method Detail
-
distortion
public double distortion()
Returns the distortion.
-
getCentroids
public double[][] getCentroids()
Returns the centroids.- Specified by:
getCentroidsin interfaceICentroidClustering- Returns:
- Centroids.
-
getClusters
public int getClusters()
-
setCluster
public void setCluster(int k)
-
Compute
public void Compute(DatasetClassification dataset)
Description copied from interface:ICentroidClusteringCompute the data.- Specified by:
Computein interfaceICentroidClustering- Parameters:
dataset- Dataset.
-
Compute
public void Compute(double[][] input)
Description copied from interface:ICentroidClusteringCompute the data.- Specified by:
Computein interfaceICentroidClustering- Parameters:
input- Data.
-
Predict
public int Predict(double[] x)
Cluster a new instance.- Specified by:
Predictin interfaceICentroidClustering- Parameters:
x- a new instance.- Returns:
- the cluster label, which is the index of nearest centroid.
-
lloyd
public static KMeans lloyd(double[][] input, int k)
The implementation of Lloyd algorithm as a benchmark. The data may contain missing values (i.e. Double.NaN). The algorithm runs up to 100 iterations.- Parameters:
input- the input data of which each row is a sample.k- the number of clusters.
-
lloyd
public static KMeans lloyd(double[][] input, int k, int maxIteration)
The implementation of Lloyd algorithm as a benchmark. The data may contain missing values (i.e. Double.NaN).- Parameters:
input- the input data of which each row is a sample.k- the number of clusters.maxIteration- the maximum number of iterations for each running.
-
lloyd
public static KMeans lloyd(double[][] input, int k, int maxIteration, int runs)
The implementation of Lloyd algorithm as a benchmark. Run the algorithm multiple times and return the best one in terms of smallest distortion. The data may contain missing values (i.e. Double.NaN).- Parameters:
data- the input data of which each row is a sample.k- the number of clusters.maxIteration- the maximum number of iterations for each running.runs- the number of runs of K-Means algorithm.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-
DataMelt 3.0 © DataMelt by jWork.ORG