Skip to content

BUG: Fix divide-by-zero and over-generation in KMeansSMOTE - #1189

Open
saitejabandaru-in wants to merge 1 commit into
scikit-learn-contrib:masterfrom
saitejabandaru-in:fix/kmeans-smote-divide-by-zero-and-rounding
Open

BUG: Fix divide-by-zero and over-generation in KMeansSMOTE#1189
saitejabandaru-in wants to merge 1 commit into
scikit-learn-contrib:masterfrom
saitejabandaru-in:fix/kmeans-smote-divide-by-zero-and-rounding

Conversation

@saitejabandaru-in

Copy link
Copy Markdown

Summary

Fixes #1186

KMeansSMOTE has two numerical issues that cause it to crash or generate the wrong number of samples:

  1. When computing cluster sparsity (_find_cluster_sparsity), it divides by non_diag_elements. If X contains only one sample (or identically 1 valid point), non_diag_elements is 0, causing a divide-by-zero error.
  2. When determining cluster_n_samples, it uses math.ceil(). If there are many valid clusters, taking the ceiling of every fractional sample count guarantees that the sum of generated samples will significantly exceed the requested n_samples.

Fix

  1. Add a check if non_diag_elements == 0: return 0.0 in _find_cluster_sparsity.
  2. Change math.ceil to round for cluster_n_samples to minimize overshoot. (Any exact sample distribution can be further refined, but round is standard for integer weighting).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] KMeansSMOTE fails when clusters have identical points and generates more samples than requested

1 participant