Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX BisectingKMeans crashes randomly #25563

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/whats_new/v1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ Changes impacting all modules
Changelog
---------

:mod:`sklearn.cluster`
......................

- |Fix| Fixed a bug in :class:`cluster.BisectingKMeans`, preventing `fit` to randomly
fail due to a permutation of the labels when running multiple inits.
:pr:`25563` by :user:`Jérémie du Boisberranger <jeremiedbb>`.

:mod:`sklearn.isotonic`
.......................

Expand Down
4 changes: 3 additions & 1 deletion sklearn/cluster/_bisect_k_means.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ def _bisect(self, X, x_squared_norms, sample_weight, cluster_to_bisect):
X, best_centers, best_labels, sample_weight
)
else: # bisecting_strategy == "largest_cluster"
scores = np.bincount(best_labels)
# Using minlength to make sure that we have the counts for both labels even
# if all samples are labelled 0.
scores = np.bincount(best_labels, minlength=2)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a comment above mentioning why we need minlength?

Copy link
Member

@lesteve lesteve Feb 7, 2023

Choose a reason for hiding this comment

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

Just to be sure: minlength=2 at this point we just have two possible labels (0 and 1) since this is BisectingKMeans right?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes


cluster_to_bisect.split(best_labels, best_centers, scores)

Expand Down