Skip to content

Commit 8e767d2

Browse files
committed
MAINT avoid FutureWarning in test
1 parent 2bc9601 commit 8e767d2

File tree

8 files changed

+51
-32
lines changed

8 files changed

+51
-32
lines changed

imblearn/over_sampling/_smote/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,13 @@ def _fit_resample(self, X, y):
548548
dtype_ohe = X_continuous.dtype
549549
else:
550550
dtype_ohe = np.float64
551-
self.ohe_ = OneHotEncoder(sparse=True, handle_unknown="ignore", dtype=dtype_ohe)
551+
552+
self.ohe_ = OneHotEncoder(handle_unknown="ignore", dtype=dtype_ohe)
553+
if hasattr(self.ohe_, "sparse_output"):
554+
# scikit-learn >= 1.2
555+
self.ohe_.set_params(sparse_output=True)
556+
else:
557+
self.ohe_.set_params(sparse=True)
552558

553559
# the input of the OneHotEncoder needs to be dense
554560
X_ohe = self.ohe_.fit_transform(

imblearn/over_sampling/_smote/cluster.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ class KMeansSMOTE(BaseSMOTE):
125125
>>> y = np.append(y, 0)
126126
>>> # Make this a binary classification problem
127127
>>> y = y == 1
128-
>>> sm = KMeansSMOTE(random_state=42)
128+
>>> sm = KMeansSMOTE(
129+
... kmeans_estimator=MiniBatchKMeans(n_init=1, random_state=0), random_state=42
130+
... )
129131
>>> X_res, y_res = sm.fit_resample(X, y)
130132
>>> # Find the number of new samples in the middle blob
131133
>>> n_res_in_middle = ((X_res[:, 0] > -5) & (X_res[:, 0] < 5)).sum()

imblearn/over_sampling/_smote/tests/test_kmeans_smote.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def data():
3838
return X, y
3939

4040

41+
@pytest.mark.filterwarnings("ignore:The default value of `n_init` will change")
4142
def test_kmeans_smote(data):
4243
X, y = data
4344
kmeans_smote = KMeansSMOTE(
@@ -59,13 +60,14 @@ def test_kmeans_smote(data):
5960
assert "batch_size" in kmeans_smote.kmeans_estimator_.get_params()
6061

6162

63+
@pytest.mark.filterwarnings("ignore:The default value of `n_init` will change")
6264
@pytest.mark.parametrize("k_neighbors", [2, NearestNeighbors(n_neighbors=3)])
6365
@pytest.mark.parametrize(
6466
"kmeans_estimator",
6567
[
6668
3,
67-
KMeans(n_clusters=3, random_state=42),
68-
MiniBatchKMeans(n_clusters=3, random_state=42),
69+
KMeans(n_clusters=3, n_init=1, random_state=42),
70+
MiniBatchKMeans(n_clusters=3, n_init=1, random_state=42),
6971
],
7072
)
7173
def test_sample_kmeans_custom(data, k_neighbors, kmeans_estimator):
@@ -83,6 +85,7 @@ def test_sample_kmeans_custom(data, k_neighbors, kmeans_estimator):
8385
assert kmeans_smote.kmeans_estimator_.n_clusters == 3
8486

8587

88+
@pytest.mark.filterwarnings("ignore:The default value of `n_init` will change")
8689
def test_sample_kmeans_not_enough_clusters(data):
8790
X, y = data
8891
smote = KMeansSMOTE(cluster_balance_threshold=10, random_state=42)
@@ -97,6 +100,7 @@ def test_sample_kmeans_density_estimation(density_exponent, cluster_balance_thre
97100
n_samples=10_000, n_classes=2, weights=[0.3, 0.7], random_state=42
98101
)
99102
smote = KMeansSMOTE(
103+
kmeans_estimator=MiniBatchKMeans(n_init=1, random_state=42),
100104
random_state=0,
101105
density_exponent=density_exponent,
102106
cluster_balance_threshold=cluster_balance_threshold,

imblearn/over_sampling/tests/test_common.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
import pytest
5+
from sklearn.cluster import MiniBatchKMeans
56

67
from imblearn.over_sampling import (
78
ADASYN,
@@ -67,7 +68,13 @@ def test_smote_m_neighbors(numerical_data, smote):
6768
[
6869
(ADASYN(random_state=0), "n_neighbors"),
6970
(BorderlineSMOTE(random_state=0), "k_neighbors"),
70-
(KMeansSMOTE(random_state=1), "k_neighbors"),
71+
(
72+
KMeansSMOTE(
73+
kmeans_estimator=MiniBatchKMeans(n_init=1, random_state=0),
74+
random_state=1,
75+
),
76+
"k_neighbors",
77+
),
7178
(SMOTE(random_state=0), "k_neighbors"),
7279
(SVMSMOTE(random_state=0), "k_neighbors"),
7380
],

imblearn/under_sampling/_prototype_generation/_cluster_centroids.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,18 @@ class ClusterCentroids(BaseUnderSampler):
9393
9494
>>> from collections import Counter
9595
>>> from sklearn.datasets import make_classification
96-
>>> from imblearn.under_sampling import \
97-
ClusterCentroids # doctest:
96+
>>> from sklearn.cluster import MiniBatchKMeans
97+
>>> from imblearn.under_sampling import ClusterCentroids
9898
>>> X, y = make_classification(n_classes=2, class_sep=2,
9999
... weights=[0.1, 0.9], n_informative=3, n_redundant=1, flip_y=0,
100100
... n_features=20, n_clusters_per_class=1, n_samples=1000, random_state=10)
101101
>>> print('Original dataset shape %s' % Counter(y))
102102
Original dataset shape Counter({{1: 900, 0: 100}})
103-
>>> cc = ClusterCentroids(random_state=42)
103+
>>> cc = ClusterCentroids(
104+
... estimator=MiniBatchKMeans(n_init=1, random_state=0), random_state=42
105+
... )
104106
>>> X_res, y_res = cc.fit_resample(X, y)
105107
>>> print('Resampled dataset shape %s' % Counter(y_res))
106-
... # doctest:
107108
Resampled dataset shape Counter({{...}})
108109
"""
109110

imblearn/under_sampling/_prototype_generation/tests/test_cluster_centroids.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
@pytest.mark.parametrize(
3434
"X, expected_voting", [(X, "soft"), (sparse.csr_matrix(X), "hard")]
3535
)
36+
@pytest.mark.filterwarnings("ignore:The default value of `n_init` will change")
3637
def test_fit_resample_check_voting(X, expected_voting):
3738
cc = ClusterCentroids(random_state=RND_SEED)
3839
cc.fit_resample(X, Y)
3940
assert cc.voting_ == expected_voting
4041

4142

43+
@pytest.mark.filterwarnings("ignore:The default value of `n_init` will change")
4244
def test_fit_resample_auto():
4345
sampling_strategy = "auto"
4446
cc = ClusterCentroids(sampling_strategy=sampling_strategy, random_state=RND_SEED)
@@ -47,6 +49,7 @@ def test_fit_resample_auto():
4749
assert y_resampled.shape == (6,)
4850

4951

52+
@pytest.mark.filterwarnings("ignore:The default value of `n_init` will change")
5053
def test_fit_resample_half():
5154
sampling_strategy = {0: 3, 1: 6}
5255
cc = ClusterCentroids(sampling_strategy=sampling_strategy, random_state=RND_SEED)
@@ -55,6 +58,7 @@ def test_fit_resample_half():
5558
assert y_resampled.shape == (9,)
5659

5760

61+
@pytest.mark.filterwarnings("ignore:The default value of `n_init` will change")
5862
def test_multiclass_fit_resample():
5963
y = Y.copy()
6064
y[5] = 2
@@ -69,7 +73,7 @@ def test_multiclass_fit_resample():
6973

7074
def test_fit_resample_object():
7175
sampling_strategy = "auto"
72-
cluster = KMeans(random_state=RND_SEED)
76+
cluster = KMeans(random_state=RND_SEED, n_init=1)
7377
cc = ClusterCentroids(
7478
sampling_strategy=sampling_strategy,
7579
random_state=RND_SEED,
@@ -84,7 +88,7 @@ def test_fit_resample_object():
8488
def test_fit_hard_voting():
8589
sampling_strategy = "auto"
8690
voting = "hard"
87-
cluster = KMeans(random_state=RND_SEED)
91+
cluster = KMeans(random_state=RND_SEED, n_init=1)
8892
cc = ClusterCentroids(
8993
sampling_strategy=sampling_strategy,
9094
random_state=RND_SEED,
@@ -111,6 +115,7 @@ def test_fit_resample_error(cluster_centroids_params, err_msg):
111115
cc.fit_resample(X, Y)
112116

113117

118+
@pytest.mark.filterwarnings("ignore:The default value of `n_init` will change")
114119
def test_cluster_centroids_hard_target_class():
115120
# check that the samples selecting by the hard voting corresponds to the
116121
# targeted class

imblearn/utils/_show_versions.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
# Author: Alexander L. Hayes <[email protected]>
99
# License: MIT
1010

11-
import importlib
12-
import sys
11+
from .. import __version__
1312

1413

1514
def _get_deps_info():
@@ -20,35 +19,30 @@ def _get_deps_info():
2019
version information on relevant Python libraries
2120
"""
2221
deps = [
22+
"imbalanced-learn",
2323
"pip",
2424
"setuptools",
25-
"imblearn",
26-
"sklearn",
2725
"numpy",
2826
"scipy",
27+
"scikit-learn",
2928
"Cython",
3029
"pandas",
3130
"keras",
3231
"tensorflow",
3332
"joblib",
3433
]
3534

36-
def get_version(module):
37-
return module.__version__
35+
deps_info = {
36+
"imbalanced-learn": __version__,
37+
}
3838

39-
deps_info = {}
39+
from importlib.metadata import PackageNotFoundError, version
4040

4141
for modname in deps:
4242
try:
43-
if modname in sys.modules:
44-
mod = sys.modules[modname]
45-
else:
46-
mod = importlib.import_module(modname)
47-
ver = get_version(mod)
48-
deps_info[modname] = ver
49-
except ImportError:
43+
deps_info[modname] = version(modname)
44+
except PackageNotFoundError:
5045
deps_info[modname] = None
51-
5246
return deps_info
5347

5448

imblearn/utils/tests/test_show_versions.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def test_get_deps_info():
99
_deps_info = _get_deps_info()
1010
assert "pip" in _deps_info
1111
assert "setuptools" in _deps_info
12-
assert "imblearn" in _deps_info
13-
assert "sklearn" in _deps_info
12+
assert "imbalanced-learn" in _deps_info
13+
assert "scikit-learn" in _deps_info
1414
assert "numpy" in _deps_info
1515
assert "scipy" in _deps_info
1616
assert "Cython" in _deps_info
@@ -26,8 +26,8 @@ def test_show_versions_default(capsys):
2626
assert "machine" in out
2727
assert "pip" in out
2828
assert "setuptools" in out
29-
assert "imblearn" in out
30-
assert "sklearn" in out
29+
assert "imbalanced-learn" in out
30+
assert "scikit-learn" in out
3131
assert "numpy" in out
3232
assert "scipy" in out
3333
assert "Cython" in out
@@ -48,8 +48,8 @@ def test_show_versions_github(capsys):
4848
assert "**Python Dependencies**" in out
4949
assert "* pip" in out
5050
assert "* setuptools" in out
51-
assert "* imblearn" in out
52-
assert "* sklearn" in out
51+
assert "* imbalanced-learn" in out
52+
assert "* scikit-learn" in out
5353
assert "* numpy" in out
5454
assert "* scipy" in out
5555
assert "* Cython" in out

0 commit comments

Comments
 (0)