{% extends "base.html" %} {% block title %} kmeans {% endblock %} {% block description %}
Finds centers of clusters and groups input samples around the clusters.
{% endblock %} {% block signature %}cv2.kmeans(data, K, bestLabels, criteria, attempts, flags[, centers]) → retval, bestLabels, centers{% endblock %} {% block parameters %}
cv2.TERM_CRITERIA_*
): The algorithm termination criteria, that is, the maximum number of iterations and/or the desired accuracy. See note below.cv2.KMEANS_*
): Flag that can take the following values:
KMEANS_RANDOM_CENTERS
: Select random initial centers in each attempt.KMEANS_PP_CENTERS
: Use kmeans++
center initialization by Arthur and Vassilvitskii [Arthur2007].KMEANS_USE_INITIAL_LABELS
: During the first (and possibly the only) attempt, use the user-supplied labels instead of computing them from the initial centers. For the second and further attempts, use the random or semi-random centers. Use one of KMEANS_*_CENTERS
flag to specify the exact method.
The function kmeans
implements a k-means algorithm that finds the centers of K
clusters and groups the input samples around the clusters. As an output, bestLabels[i]
contains a 0-based cluster index for the sample stored in the \(i^{th}\) row of the samples matrix.
The function returns retVal
, the compactness measure that is computed as $$\sum_i \| \text{samples}_i - \text{centers}_{\text{labels}_i} \|^2$$
after every attempt. The best (minimum) value is chosen and the corresponding labels and the compactness value are returned by the function.
criteria
of type TermCriteria
contains three pieces of important information:
criteria.type
: The type of termination criteria. One of iterations (cv2.TERM_CRITERIA_COUNT
), epsilon (cv2.TERM_CRITERIA_EPS
), or both (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT
; will stop when first is met).criteria.maxCount
(criteria.epsilon
(criteria
, even if only using one stopping condition.flags = KMEANS_USE_INITIAL_LABELS
) flag, and then choose the best (most-compact) clustering.