Finds centers of clusters and groups input samples around the clusters.
double kmeans
(InputArray data, int K, InputOutputArray bestLabels, TermCriteria criteria, int attempts, int flags, OutputArray centers=noArray() )¶
cv2.
kmeans
(data, K, criteria, attempts, flags[, bestLabels[, centers]]) → retval, bestLabels, centers¶
int cvKMeans2
(const CvArr* samples, int cluster_count, CvArr* labels, CvTermCriteria termcrit, int attempts=1, CvRNG* rng=0, int flags=0, CvArr* _centers=0, double* compactness=0 )¶
cv.
KMeans2
(samples, nclusters, labels, termcrit, attempts=1, flags=0, centers=None) → float¶Parameters: 


The function kmeans
implements a kmeans algorithm that finds the
centers of cluster_count
clusters and groups the input samples
around the clusters. As an output,
contains a 0based cluster index for
the sample stored in the
row of the samples
matrix.
The function returns the compactness measure that is computed as
after every attempt. The best (minimum) value is chosen and the
corresponding labels and the compactness value are returned by the function.
Basically, you can use only the core of the function, set the number of
attempts to 1, initialize labels each time using a custom algorithm, pass them with the
( flags
= KMEANS_USE_INITIAL_LABELS
) flag, and then choose the best (mostcompact) clustering.
Note
Splits an element set into equivalency classes.
template<typename _Tp, class _EqPredicate> int partition
(const vector<_Tp>& vec, vector<int>& labels, _EqPredicate predicate=_EqPredicate())¶Parameters: 


The generic function partition
implements an
algorithm for
splitting a set of
elements into one or more equivalency classes, as described in
http://en.wikipedia.org/wiki/Disjointset_data_structure
. The function
returns the number of equivalency classes.
[Arthur2007]  Arthur and S. Vassilvitskii. kmeans++: the advantages of careful seeding, Proceedings of the eighteenth annual ACMSIAM symposium on Discrete algorithms, 2007 