Skip to content

Commit ecab162

Browse files
authored
MAINT: split test for different SMOTE variants (#485)
1 parent 2d4902e commit ecab162

File tree

3 files changed

+85
-36
lines changed

3 files changed

+85
-36
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
import numpy as np
3+
4+
from sklearn.neighbors import NearestNeighbors
5+
from sklearn.utils.testing import assert_allclose
6+
from sklearn.utils.testing import assert_array_equal
7+
8+
from imblearn.over_sampling import BorderlineSMOTE
9+
10+
11+
@pytest.fixture
12+
def data():
13+
X = np.array([[0.11622591, -0.0317206], [0.77481731, 0.60935141],
14+
[1.25192108, -0.22367336], [0.53366841, -0.30312976],
15+
[1.52091956, -0.49283504], [-0.28162401, -2.10400981],
16+
[0.83680821, 1.72827342], [0.3084254, 0.33299982],
17+
[0.70472253, -0.73309052], [0.28893132, -0.38761769],
18+
[1.15514042, 0.0129463], [0.88407872, 0.35454207],
19+
[1.31301027, -0.92648734], [-1.11515198, -0.93689695],
20+
[-0.18410027, -0.45194484], [0.9281014, 0.53085498],
21+
[-0.14374509, 0.27370049], [-0.41635887, -0.38299653],
22+
[0.08711622, 0.93259929], [1.70580611, -0.11219234]])
23+
y = np.array([0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0])
24+
return X, y
25+
26+
27+
def test_borderline_smote_wrong_kind(data):
28+
bsmote = BorderlineSMOTE(kind='rand')
29+
with pytest.raises(ValueError, match='The possible "kind" of algorithm'):
30+
bsmote.fit_resample(*data)
31+
32+
33+
@pytest.mark.parametrize('kind', ['borderline-1', 'borderline-2'])
34+
def test_borderline_smote(kind, data):
35+
bsmote = BorderlineSMOTE(kind=kind, random_state=42)
36+
bsmote_nn = BorderlineSMOTE(kind=kind, random_state=42,
37+
k_neighbors=NearestNeighbors(n_neighbors=6),
38+
m_neighbors=NearestNeighbors(n_neighbors=11))
39+
40+
X_res_1, y_res_1 = bsmote.fit_resample(*data)
41+
X_res_2, y_res_2 = bsmote_nn.fit_resample(*data)
42+
43+
assert_allclose(X_res_1, X_res_2)
44+
assert_array_equal(y_res_1, y_res_2)

imblearn/over_sampling/tests/test_smote.py

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
from sklearn.svm import SVC
1212

1313
from imblearn.over_sampling import SMOTE
14-
from imblearn.over_sampling import BorderlineSMOTE
15-
from imblearn.over_sampling import SVMSMOTE
14+
1615

1716
RND_SEED = 0
1817
X = np.array([[0.11622591, -0.0317206], [0.77481731, 0.60935141],
@@ -287,37 +286,3 @@ def test_sample_with_nn_svm():
287286
1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0])
288287
assert_allclose(X_resampled, X_gt, rtol=R_TOL)
289288
assert_array_equal(y_resampled, y_gt)
290-
291-
292-
def test_borderline_smote_wrong_kind():
293-
bsmote = BorderlineSMOTE(kind='rand')
294-
with pytest.raises(ValueError, match='The possible "kind" of algorithm'):
295-
bsmote.fit_resample(X, Y)
296-
297-
298-
@pytest.mark.parametrize('kind', ['borderline-1', 'borderline-2'])
299-
def test_borderline_smote(kind):
300-
bsmote = BorderlineSMOTE(kind=kind, random_state=42)
301-
bsmote_nn = BorderlineSMOTE(kind=kind, random_state=42,
302-
k_neighbors=NearestNeighbors(n_neighbors=6),
303-
m_neighbors=NearestNeighbors(n_neighbors=11))
304-
305-
X_res_1, y_res_1 = bsmote.fit_resample(X, Y)
306-
X_res_2, y_res_2 = bsmote_nn.fit_resample(X, Y)
307-
308-
assert_allclose(X_res_1, X_res_2)
309-
assert_array_equal(y_res_1, y_res_2)
310-
311-
312-
def test_svm_smote():
313-
svm_smote = SVMSMOTE(random_state=42)
314-
svm_smote_nn = SVMSMOTE(random_state=42,
315-
k_neighbors=NearestNeighbors(n_neighbors=6),
316-
m_neighbors=NearestNeighbors(n_neighbors=11),
317-
svm_estimator=SVC(gamma='scale', random_state=42))
318-
319-
X_res_1, y_res_1 = svm_smote.fit_resample(X, Y)
320-
X_res_2, y_res_2 = svm_smote_nn.fit_resample(X, Y)
321-
322-
assert_allclose(X_res_1, X_res_2)
323-
assert_array_equal(y_res_1, y_res_2)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
import numpy as np
3+
4+
from sklearn.neighbors import NearestNeighbors
5+
from sklearn.svm import SVC
6+
7+
from sklearn.utils.testing import assert_allclose
8+
from sklearn.utils.testing import assert_array_equal
9+
10+
from imblearn.over_sampling import SVMSMOTE
11+
12+
13+
@pytest.fixture
14+
def data():
15+
X = np.array([[0.11622591, -0.0317206], [0.77481731, 0.60935141],
16+
[1.25192108, -0.22367336], [0.53366841, -0.30312976],
17+
[1.52091956, -0.49283504], [-0.28162401, -2.10400981],
18+
[0.83680821, 1.72827342], [0.3084254, 0.33299982],
19+
[0.70472253, -0.73309052], [0.28893132, -0.38761769],
20+
[1.15514042, 0.0129463], [0.88407872, 0.35454207],
21+
[1.31301027, -0.92648734], [-1.11515198, -0.93689695],
22+
[-0.18410027, -0.45194484], [0.9281014, 0.53085498],
23+
[-0.14374509, 0.27370049], [-0.41635887, -0.38299653],
24+
[0.08711622, 0.93259929], [1.70580611, -0.11219234]])
25+
y = np.array([0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0])
26+
return X, y
27+
28+
29+
def test_svm_smote(data):
30+
svm_smote = SVMSMOTE(random_state=42)
31+
svm_smote_nn = SVMSMOTE(random_state=42,
32+
k_neighbors=NearestNeighbors(n_neighbors=6),
33+
m_neighbors=NearestNeighbors(n_neighbors=11),
34+
svm_estimator=SVC(gamma='scale', random_state=42))
35+
36+
X_res_1, y_res_1 = svm_smote.fit_resample(*data)
37+
X_res_2, y_res_2 = svm_smote_nn.fit_resample(*data)
38+
39+
assert_allclose(X_res_1, X_res_2)
40+
assert_array_equal(y_res_1, y_res_2)

0 commit comments

Comments
 (0)