FIX Elkan k-means does not stop if tol=0 #16075
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
K-means convergence is still different between "full" k-means and "elkan" k-means.
The fix in #15831 is incomplete. Compare:
scikit-learn/sklearn/cluster/_kmeans.py
Lines 441 to 442 in 4fe4d27
and
scikit-learn/sklearn/cluster/_k_means_elkan.pyx
Line 233 in 4fe4d27
scikit-learn/sklearn/cluster/_k_means_elkan.pyx
Lines 249 to 250 in 4fe4d27
it should be noted that in the second version, it would likely make sense to first use
squared_norm
, and then separate the square root, rather than taking the square of the rooted values below.But in this PR I'm just pointing to a single character. One tests
<=
and the other tests<
.With
tol=0
this means that "full" may stop when the clusters stop moving, while withelkan
it never stops then, but always takes all iterations.I do not think this is the best stopping criterion. If a numerical issue arises in computing the center shifts, this may cause the algorithm to always take the maximum number of iterations. The classic termination criterion for k-means is different: stop if no object is relabeled. That is more reliable.