BalancedKMeans Class 
Namespace: Accord.MachineLearning
The BalancedKMeans type exposes the following members.
Name  Description  

BalancedKMeans(Int32) 
Initializes a new instance of the Balanced KMeans algorithm.
 
BalancedKMeans(Int32, IDistanceDouble) 
Initializes a new instance of the Balanced KMeans algorithm.

Name  Description  

Centroids 
Gets or sets the cluster centroids.
(Inherited from KMeans.)  
Clusters 
Gets the clusters found by Kmeans.
(Inherited from KMeans.)  
ComputeCovariances 
Gets or sets whether covariance matrices for the clusters should
be computed at the end of an iteration. Default is true.
(Inherited from KMeans.)  
ComputeError 
Gets or sets whether the clustering distortion error (the
average distance between all data points and the cluster
centroids) should be computed at the end of the algorithm.
The result will be stored in Error. Default is true.
(Inherited from KMeans.)  
Dimension 
Gets the dimensionality of the data space.
(Inherited from KMeans.)  
Distance 
Gets or sets the distance function used
as a distance metric between data points.
(Inherited from KMeans.)  
Error 
Gets the cluster distortion error after the
last call to this class' Compute methods.
(Inherited from KMeans.)  
Iterations 
Gets the number of iterations performed in the
last call to this class' Compute methods.
(Inherited from KMeans.)  
K 
Gets the number of clusters.
(Inherited from KMeans.)  
Labels 
Gets the labels assigned for each data point in the last
call to Learn(Double, Double).
 
MaxIterations 
Gets or sets the maximum number of iterations to
be performed by the method. If set to zero, no
iteration limit will be imposed. Default is 0.
(Inherited from KMeans.)  
ParallelOptions 
Gets or sets the parallelization options for this algorithm.
(Inherited from ParallelLearningBase.)  
Token 
Gets or sets a cancellation token that can be used
to cancel the algorithm while it is running.
(Inherited from ParallelLearningBase.)  
Tolerance 
Gets or sets the relative convergence threshold
for stopping the algorithm. Default is 1e5.
(Inherited from KMeans.)  
UseSeeding 
Gets or sets the strategy used to initialize the
centroids of the clustering algorithm. Default is
KMeansPlusPlus.
(Inherited from KMeans.) 
Name  Description  

Compute(Double)  Obsolete.
Divides the input data into K clusters.
(Inherited from KMeans.)  
Compute(Double, Double)  Obsolete.
Divides the input data into K clusters.
(Inherited from KMeans.)  
Compute(Double, Double)  Obsolete.
Divides the input data into K clusters.
(Inherited from KMeans.)  
ComputeInformation(Double) 
Computes the information about each cluster (covariance, proportions and error).
(Inherited from KMeans.)  
ComputeInformation(Double, Int32) 
Computes the information about each cluster (covariance, proportions and error).
(Inherited from KMeans.)  
converged 
Determines if the algorithm has converged by comparing the
centroids between two consecutive iterations.
(Inherited from KMeans.)  
Equals  Determines whether the specified object is equal to the current object. (Inherited from Object.)  
Finalize  Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)  
GetHashCode  Serves as the default hash function. (Inherited from Object.)  
GetType  Gets the Type of the current instance. (Inherited from Object.)  
Learn 
Learns a model that can map the given inputs to the desired outputs. Note:
the model created by this function will not be able to produce balanced
clusterings. To retrieve the balanced labels, check the Labels
property of this class after calling this function.
(Overrides KMeansLearn(Double, Double).)  
MemberwiseClone  Creates a shallow copy of the current Object. (Inherited from Object.)  
Randomize 
Randomizes the clusters inside a dataset.
(Inherited from KMeans.)  
ToString  Returns a string that represents the current object. (Inherited from Object.) 
Name  Description  

HasMethod 
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)  
IsEqual 
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.)  
To(Type)  Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)  
ToT  Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) 
The Balanced kMeans algorithm attempts to find a clustering where each cluster has approximately the same number of data points. The Balanced kMeans implementation used in the framework uses the Munkres algorithm to solve the assignment problem thus enforcing balance between the clusters.
Note: the Learn(Double, Double) method of this class will return the centroids of balanced clusters, but please note that these centroids cannot be used to obtain balanced clusterings for another (or even the same) data set. Instead, in order to inspect the balanced clustering that has been obtained after calling Learn(Double, Double), please take a look at the contents of the Labels property.
Accord.Math.Random.Generator.Seed = 0; // Declare some observations double[][] observations = { new double[] { 5, 2, 1 }, new double[] { 5, 5, 6 }, new double[] { 2, 1, 1 }, new double[] { 1, 1, 2 }, new double[] { 1, 2, 2 }, new double[] { 3, 1, 2 }, new double[] { 11, 5, 4 }, new double[] { 15, 5, 6 }, new double[] { 10, 5, 6 }, }; double[][] orig = observations.MemberwiseClone(); // Create a new KMeans algorithm with 3 clusters BalancedKMeans kmeans = new BalancedKMeans(3) { // Note: in balanced kmeans the chances of the algorithm oscillating // between two solutions increases considerably. For this reason, we // set a maxiterations limit to avoid iterating indefinitely. MaxIterations = 100 }; // Compute the algorithm, retrieving an integer array // containing the labels for each of the observations KMeansClusterCollection clusters = kmeans.Learn(observations); // As a result, the first two observations should belong to the // same cluster (thus having the same label). The same should // happen to the next four observations and to the last three. int[] labels = clusters.Decide(observations);