Skip to content

Commit 2f7e4e5

Browse files
authored
FIX consider only current class when generating sample in ADASYN (#355)
1 parent 3d98f16 commit 2f7e4e5

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

doc/whats_new/v0.0.4.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ Version 0.4 (under development)
66
Changelog
77
---------
88

9+
Bug fixes
10+
.........
11+
12+
- Fix bug in ADASYN to consider only samples from the current class when
13+
generating new samples. :issue:`354` by :user:`Guillaume Lemaitre
14+
<glemaitre>`.
15+
916
Maintenance
1017
...........
1118

imblearn/over_sampling/adasyn.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ def _sample(self, X, y):
184184
steps = random_state.uniform(size=len(nn_zs))
185185
if x_i.nnz:
186186
for step, nn_z in zip(steps, nn_zs):
187-
sample = x_i + step * (X[x_i_nn[nn_z], :] - x_i)
187+
sample = (x_i +
188+
step * (X_class[x_i_nn[nn_z], :] - x_i))
188189
row_indices += ([n_samples_generated] *
189190
len(sample.indices))
190191
col_indices += sample.indices.tolist()
@@ -204,7 +205,8 @@ def _sample(self, X, y):
204205
nn_zs = random_state.randint(
205206
1, high=self.nn_.n_neighbors, size=num_sample_i)
206207
steps = random_state.uniform(size=len(nn_zs))
207-
x_class_gen.append([x_i + step * (X[x_i_nn[nn_z], :] - x_i)
208+
x_class_gen.append([x_i +
209+
step * (X_class[x_i_nn[nn_z], :] - x_i)
208210
for step, nn_z in zip(steps, nn_zs)])
209211

210212
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)