Skip to content

FIX Improve best run detection in kmeans when n_init > 1 #21195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 5, 2021

Conversation

jeremiedbb
Copy link
Member

Fixes #21160

Instead of relying of some tolerance to take rouding errors into account, we can simply check that the clustering of the new run is the same as the clustering of the best run so far (up to a permutation of the labels).

The check is really quick: ~200ms for 100_000_000 samples, which is negligible compared to a complete run of kmeans.

The non regression test is already there: test_k_means_fit_predict (see the linked issue), even if it does not fail in the CI because it's an effect quite hard to trigger. I think checking that it now passes is enough. @lesteve could you confirm that ?

@lesteve
Copy link
Member

lesteve commented Sep 29, 2021

It seems to fix it indeed on my machine:

  • on this PR: no error when running 100 times one of the failing tests like this
for i in $(seq 1 100); do
    pytest sklearn/cluster/tests/test_k_means.py -k 'test_k_means_fit_predict and csr_matrix and float32 and 4-300 and full' -q; done
  • on main: with this same test I get roughly 1 failure for 4 runs

@lesteve
Copy link
Member

lesteve commented Sep 29, 2021

I guess it would be great to have a few tests for _is_same_clustering.

@jjerphan
Copy link
Member

It seems to fix it indeed on my machine:

* on this PR: no error when running 100 times one of the failing tests like this
for i in $(seq 1 100); do
    pytest sklearn/cluster/tests/test_k_means.py -k 'test_k_means_fit_predict and csr_matrix and float32 and 4-300 and full' -q; done
* on `main`: with this same test I get roughly 1 failure for 4 runs

I also observe this on my machine.

Copy link
Member

@jjerphan jjerphan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you @jeremiedbb!

@lesteve
Copy link
Member

lesteve commented Oct 1, 2021

LGTM, one thing I am wondering is whether the changelog should go in v1.0.rst or v1.1.rst ...

@jeremiedbb
Copy link
Member Author

LGTM, one thing I am wondering is whether the changelog should go in v1.0.rst or v1.1.rst .

v1.0 because the next bug fix release will be 1.0.1

@lesteve lesteve changed the title FIX Improve better run detection in kmeans when n_init > 1 FIX Improve best run detection in kmeans when n_init > 1 Oct 5, 2021
@lesteve lesteve merged commit 0a88cf8 into scikit-learn:main Oct 5, 2021
@lesteve
Copy link
Member

lesteve commented Oct 5, 2021

Merging, thanks!

@jeremiedbb jeremiedbb added this to the 1.0.1 milestone Oct 5, 2021
@jeremiedbb jeremiedbb added the To backport PR merged in master that need a backport to a release branch defined based on the milestone. label Oct 5, 2021
@glemaitre glemaitre mentioned this pull request Oct 23, 2021
10 tasks
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Oct 23, 2021
samronsin pushed a commit to samronsin/scikit-learn that referenced this pull request Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cython module:cluster To backport PR merged in master that need a backport to a release branch defined based on the milestone.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

test_k_means_fit_predict failing intermittently on main
3 participants