From 67c70129d02d45f19fb6c7ce8856cd049eb08216 Mon Sep 17 00:00:00 2001 From: Sai Teja Bandaru Date: Tue, 21 Jul 2026 22:56:56 +0200 Subject: [PATCH] fix: prevent divide-by-zero in KMeansSMOTE and fix over-generation due to math.ceil --- imblearn/over_sampling/_smote/cluster.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/imblearn/over_sampling/_smote/cluster.py b/imblearn/over_sampling/_smote/cluster.py index b5777a383..7a66db994 100644 --- a/imblearn/over_sampling/_smote/cluster.py +++ b/imblearn/over_sampling/_smote/cluster.py @@ -207,6 +207,8 @@ def _find_cluster_sparsity(self, X): euclidean_distances[ind, ind] = 0 non_diag_elements = (X.shape[0] ** 2) - X.shape[0] + if non_diag_elements == 0: + return 0.0 mean_distance = euclidean_distances.sum() / non_diag_elements exponent = ( math.log(X.shape[0], 1.6) ** 1.8 * 0.16 @@ -288,7 +290,7 @@ def _fit_resample(self, X, y): ] cluster_n_samples = int( - math.ceil(n_samples * cluster_weights[valid_cluster_idx]) + round(n_samples * cluster_weights[valid_cluster_idx]) ) X_new, y_new = self._make_samples(