Skip to content

Commit 16eda28

Browse files
authored
MAINT deprecate n_jobs in over-sampling algorithms (#887)
1 parent 51c56a1 commit 16eda28

File tree

5 files changed

+129
-1
lines changed

5 files changed

+129
-1
lines changed

doc/whats_new/v0.10.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ Version 0.10.0 (ongoing)
66
Changelog
77
---------
88

9+
Deprecation
10+
...........
11+
12+
- The parameter `n_jobs` has been deprecated from the classes
13+
:class:`~imblearn.over_sampling.ADASYN`,
14+
:class:`~imblearn.over_sampling.BorderlineSMOTE`,
15+
:class:`~imblearn.over_sampling.SMOTE`,
16+
:class:`~imblearn.over_sampling.SMOTENC`,
17+
:class:`~imblearn.over_sampling.SMOTEN`, and
18+
:class:`~imblearn.over_sampling.SVMSMOTE`. Instead, pass a nearest neighbors
19+
estimator where `n_jobs` is set.
20+
:pr:`887` by :user:`Guillaume Lemaitre <glemaitre>`.
21+
922
Enhancements
1023
............
1124

imblearn/over_sampling/_adasyn.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Christos Aridas
55
# License: MIT
66

7+
import warnings
8+
79
import numpy as np
810
from scipy import sparse
911

@@ -53,6 +55,12 @@ class ADASYN(BaseOverSampler):
5355
5456
{n_jobs}
5557
58+
.. deprecated:: 0.10
59+
`n_jobs` has been deprecated in 0.10 and will be removed in 0.12.
60+
It was previously used to set `n_jobs` of nearest neighbors
61+
algorithm. From now on, you can pass an estimator where `n_jobs` is
62+
already set instead.
63+
5664
Attributes
5765
----------
5866
sampling_strategy_ : dict
@@ -133,6 +141,15 @@ def _validate_estimator(self):
133141
)
134142

135143
def _fit_resample(self, X, y):
144+
# FIXME: to be removed in 0.12
145+
if self.n_jobs is not None:
146+
warnings.warn(
147+
"The parameter `n_jobs` has been deprecated in 0.10 and will be "
148+
"removed in 0.12. You can pass an nearest neighbors estimator where "
149+
"`n_jobs` is already set instead.",
150+
FutureWarning,
151+
)
152+
136153
self._validate_estimator()
137154
random_state = check_random_state(self.random_state)
138155

imblearn/over_sampling/_smote/base.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# License: MIT
88

99
import math
10+
import warnings
1011
from collections import Counter
1112

1213
import numpy as np
@@ -238,6 +239,12 @@ class SMOTE(BaseSMOTE):
238239
239240
{n_jobs}
240241
242+
.. deprecated:: 0.10
243+
`n_jobs` has been deprecated in 0.10 and will be removed in 0.12.
244+
It was previously used to set `n_jobs` of nearest neighbors
245+
algorithm. From now on, you can pass an estimator where `n_jobs` is
246+
already set instead.
247+
241248
Attributes
242249
----------
243250
sampling_strategy_ : dict
@@ -316,6 +323,15 @@ def __init__(
316323
)
317324

318325
def _fit_resample(self, X, y):
326+
# FIXME: to be removed in 0.12
327+
if self.n_jobs is not None:
328+
warnings.warn(
329+
"The parameter `n_jobs` has been deprecated in 0.10 and will be "
330+
"removed in 0.12. You can pass an nearest neighbors estimator where "
331+
"`n_jobs` is already set instead.",
332+
FutureWarning,
333+
)
334+
319335
self._validate_estimator()
320336

321337
X_resampled = [X.copy()]
@@ -388,6 +404,12 @@ class SMOTENC(SMOTE):
388404
389405
{n_jobs}
390406
407+
.. deprecated:: 0.10
408+
`n_jobs` has been deprecated in 0.10 and will be removed in 0.12.
409+
It was previously used to set `n_jobs` of nearest neighbors
410+
algorithm. From now on, you can pass an estimator where `n_jobs` is
411+
already set instead.
412+
391413
See Also
392414
--------
393415
SMOTE : Over-sample using SMOTE.
@@ -496,6 +518,15 @@ def _validate_estimator(self):
496518
)
497519

498520
def _fit_resample(self, X, y):
521+
# FIXME: to be removed in 0.12
522+
if self.n_jobs is not None:
523+
warnings.warn(
524+
"The parameter `n_jobs` has been deprecated in 0.10 and will be "
525+
"removed in 0.12. You can pass an nearest neighbors estimator where "
526+
"`n_jobs` is already set instead.",
527+
FutureWarning,
528+
)
529+
499530
self.n_features_ = X.shape[1]
500531
self._validate_estimator()
501532

@@ -636,7 +667,7 @@ def _generate_samples(self, X, nn_data, nn_num, rows, cols, steps):
636667
class SMOTEN(SMOTE):
637668
"""Synthetic Minority Over-sampling Technique for Nominal.
638669
639-
This method is refered as SMOTEN in [1]_. It expects that the data to
670+
This method is referred as SMOTEN in [1]_. It expects that the data to
640671
resample are only made of categorical features.
641672
642673
Read more in the :ref:`User Guide <smote_adasyn>`.
@@ -664,6 +695,12 @@ class SMOTEN(SMOTE):
664695
665696
{n_jobs}
666697
698+
.. deprecated:: 0.10
699+
`n_jobs` has been deprecated in 0.10 and will be removed in 0.12.
700+
It was previously used to set `n_jobs` of nearest neighbors
701+
algorithm. From now on, you can pass an estimator where `n_jobs` is
702+
already set instead.
703+
667704
Attributes
668705
----------
669706
sampling_strategy_ : dict
@@ -755,6 +792,15 @@ def _make_samples(self, X_class, klass, y_dtype, nn_indices, n_samples):
755792
return X_new, y_new
756793

757794
def _fit_resample(self, X, y):
795+
# FIXME: to be removed in 0.12
796+
if self.n_jobs is not None:
797+
warnings.warn(
798+
"The parameter `n_jobs` has been deprecated in 0.10 and will be "
799+
"removed in 0.12. You can pass an nearest neighbors estimator where "
800+
"`n_jobs` is already set instead.",
801+
FutureWarning,
802+
)
803+
758804
self._validate_estimator()
759805

760806
X_resampled = [X.copy()]

imblearn/over_sampling/_smote/filter.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# Dzianis Dudnik
77
# License: MIT
88

9+
import warnings
10+
911
import numpy as np
1012
from scipy import sparse
1113

@@ -61,6 +63,12 @@ class BorderlineSMOTE(BaseSMOTE):
6163
6264
{n_jobs}
6365
66+
.. deprecated:: 0.10
67+
`n_jobs` has been deprecated in 0.10 and will be removed in 0.12.
68+
It was previously used to set `n_jobs` of nearest neighbors
69+
algorithm. From now on, you can pass an estimator where `n_jobs` is
70+
already set instead.
71+
6472
m_neighbors : int or object, default=10
6573
The nearest neighbors used to determine if a minority sample is in
6674
"danger". You can pass:
@@ -176,6 +184,15 @@ def _validate_estimator(self):
176184
)
177185

178186
def _fit_resample(self, X, y):
187+
# FIXME: to be removed in 0.12
188+
if self.n_jobs is not None:
189+
warnings.warn(
190+
"The parameter `n_jobs` has been deprecated in 0.10 and will be "
191+
"removed in 0.12. You can pass an nearest neighbors estimator where "
192+
"`n_jobs` is already set instead.",
193+
FutureWarning,
194+
)
195+
179196
self._validate_estimator()
180197

181198
X_resampled = X.copy()
@@ -289,6 +306,12 @@ class SVMSMOTE(BaseSMOTE):
289306
290307
{n_jobs}
291308
309+
.. deprecated:: 0.10
310+
`n_jobs` has been deprecated in 0.10 and will be removed in 0.12.
311+
It was previously used to set `n_jobs` of nearest neighbors
312+
algorithm. From now on, you can pass an estimator where `n_jobs` is
313+
already set instead.
314+
292315
m_neighbors : int or object, default=10
293316
The nearest neighbors used to determine if a minority sample is in
294317
"danger". You can pass:
@@ -416,6 +439,15 @@ def _validate_estimator(self):
416439
self.svm_estimator_ = clone(self.svm_estimator)
417440

418441
def _fit_resample(self, X, y):
442+
# FIXME: to be removed in 0.12
443+
if self.n_jobs is not None:
444+
warnings.warn(
445+
"The parameter `n_jobs` has been deprecated in 0.10 and will be "
446+
"removed in 0.12. You can pass an nearest neighbors estimator where "
447+
"`n_jobs` is already set instead.",
448+
FutureWarning,
449+
)
450+
419451
self._validate_estimator()
420452
random_state = check_random_state(self.random_state)
421453
X_resampled = X.copy()

imblearn/over_sampling/tests/test_common.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,23 @@ def test_numerical_smote_extra_custom_nn(numerical_data, smote):
116116

117117
assert X_res.shape == (120, 2)
118118
assert Counter(y_res) == {0: 60, 1: 60}
119+
120+
121+
# FIXME: to be removed in 0.12
122+
@pytest.mark.parametrize(
123+
"sampler",
124+
[
125+
ADASYN(random_state=0),
126+
BorderlineSMOTE(random_state=0),
127+
SMOTE(random_state=0),
128+
SMOTEN(random_state=0),
129+
SMOTENC([0], random_state=0),
130+
SVMSMOTE(random_state=0),
131+
],
132+
)
133+
def test_n_jobs_deprecation_warning(numerical_data, sampler):
134+
X, y = numerical_data
135+
sampler.set_params(n_jobs=2)
136+
warning_msg = "The parameter `n_jobs` has been deprecated"
137+
with pytest.warns(FutureWarning, match=warning_msg):
138+
sampler.fit_resample(X, y)

0 commit comments

Comments
 (0)