FIX KMeansSMOTE: uniform weights for degenerate clusters (#1186)#1187
Open
chaoz23 wants to merge 1 commit into
Open
FIX KMeansSMOTE: uniform weights for degenerate clusters (#1186)#1187chaoz23 wants to merge 1 commit into
chaoz23 wants to merge 1 commit into
Conversation
chaoz23
force-pushed
the
fix/kmeans-smote-degenerate-clusters
branch
from
July 18, 2026 04:30
e295a4e to
a5f6f4d
Compare
When every sample within each valid cluster is identical, all cluster sparsities are 0, so dividing by their (zero) sum produced NaN weights and crashed with 'ValueError: cannot convert float NaN to integer' in math.ceil. Fall back to uniform weighting across the valid clusters. Toward scikit-learn-contrib#1186: fixes the crash (issue 1). The exact-count allocation (issue 2) is left as a follow-up, so this does not auto-close the issue.
chaoz23
force-pushed
the
fix/kmeans-smote-degenerate-clusters
branch
from
July 18, 2026 05:13
a5f6f4d to
829f723
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Toward #1186 — fixes the crash (issue 1). The exact-count allocation (issue 2) is intentionally left as a follow-up, so this does not auto-close the issue.
What
When every sample within each valid cluster is identical, all cluster sparsities are
0, socluster_sparsities / cluster_sparsities.sum()divides by zero and yieldsNaNweights. The subsequentmath.ceil(...)then raisesValueError: cannot convert float NaN to integer.This can happen with duplicated rows or discrete/one-hot features where a whole cluster collapses to a single point.
Change
0, instead of dividing by zero.valid_clustersRuntimeErrorguard ahead of the weight computation (previously it ran an unnecessary division on an empty array first).Reproducer (from the issue)
Scope
Limited to issue 1 (the crash). Issue 2 (making the total number of generated samples exactly match the requested count) is a separate behavioral change — happy to follow up on it in its own PR.