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

Skip to content

Commit ed37222

Browse files
committed
FIX BisectingKMeans crashes randomly (#25563)
Fixes #25505
1 parent dc61778 commit ed37222

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

doc/whats_new/v1.2.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ Changes impacting all modules
2727
Changelog
2828
---------
2929

30+
:mod:`sklearn.cluster`
31+
......................
32+
33+
- |Fix| Fixed a bug in :class:`cluster.BisectingKMeans`, preventing `fit` to randomly
34+
fail due to a permutation of the labels when running multiple inits.
35+
:pr:`25563` by :user:`Jérémie du Boisberranger <jeremiedbb>`.
36+
3037
:mod:`sklearn.isotonic`
3138
.......................
3239

sklearn/cluster/_bisect_k_means.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ def _bisect(self, X, x_squared_norms, sample_weight, cluster_to_bisect):
337337
X, best_centers, best_labels, sample_weight
338338
)
339339
else: # bisecting_strategy == "largest_cluster"
340-
scores = np.bincount(best_labels)
340+
# Using minlength to make sure that we have the counts for both labels even
341+
# if all samples are labelled 0.
342+
scores = np.bincount(best_labels, minlength=2)
341343

342344
cluster_to_bisect.split(best_labels, best_centers, scores)
343345

0 commit comments

Comments
 (0)