Skip to content

Commit a62cc3c

Browse files
committed
FIX consider only current class when generating sample in ADASYN (#355)
1 parent 45c7d98 commit a62cc3c

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

doc/whats_new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Bug fixes
3737
- Fixed bug for `check_ratio` to be able to pass arguments when `ratio` is a
3838
callable. By `Guillaume Lemaitre`_.`
3939

40+
- Fix bug in ADASYN to consider only samples from the current class when
41+
generating new samples. :issue:`354` by :user:`Guillaume Lemaitre
42+
<glemaitre>`.
43+
4044
New features
4145
~~~~~~~~~~~~
4246

imblearn/over_sampling/adasyn.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ def _sample(self, X, y):
197197
steps = random_state.uniform(size=len(nn_zs))
198198
if x_i.nnz:
199199
for step, nn_z in zip(steps, nn_zs):
200-
sample = x_i + step * (X[x_i_nn[nn_z], :] - x_i)
200+
sample = (x_i +
201+
step * (X_class[x_i_nn[nn_z], :] - x_i))
201202
row_indices += ([n_samples_generated] *
202203
len(sample.indices))
203204
col_indices += sample.indices.tolist()
@@ -217,7 +218,8 @@ def _sample(self, X, y):
217218
nn_zs = random_state.randint(
218219
1, high=self.nn_.n_neighbors, size=num_sample_i)
219220
steps = random_state.uniform(size=len(nn_zs))
220-
x_class_gen.append([x_i + step * (X[x_i_nn[nn_z], :] - x_i)
221+
x_class_gen.append([x_i +
222+
step * (X_class[x_i_nn[nn_z], :] - x_i)
221223
for step, nn_z in zip(steps, nn_zs)])
222224

223225
X_new = np.concatenate(x_class_gen)

imblearn/over_sampling/tests/test_adasyn.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ def test_ada_fit_sample():
6464
[-0.41635887, -0.38299653],
6565
[0.08711622, 0.93259929],
6666
[1.70580611, -0.11219234],
67-
[0.36370445, -0.19262406],
67+
[0.94899098, -0.30508981],
6868
[0.28204936, -0.13953426],
69-
[0.39635544, 0.33629036],
70-
[0.35301481, 0.25795516]])
69+
[1.58028868, -0.04089947],
70+
[0.66117333, -0.28009063]])
7171
y_gt = np.array([
7272
0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0
7373
])
@@ -106,10 +106,10 @@ def test_ada_fit_sample_nn_obj():
106106
[-0.41635887, -0.38299653],
107107
[0.08711622, 0.93259929],
108108
[1.70580611, -0.11219234],
109-
[0.36370445, -0.19262406],
109+
[0.94899098, -0.30508981],
110110
[0.28204936, -0.13953426],
111-
[0.39635544, 0.33629036],
112-
[0.35301481, 0.25795516]])
111+
[1.58028868, -0.04089947],
112+
[0.66117333, -0.28009063]])
113113
y_gt = np.array([
114114
0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0
115115
])

0 commit comments

Comments
 (0)