Skip to content

Commit 2517bd9

Browse files
committed
fixed syntax error; removed unused & variables; _safe_indexing; n_jobs docstring substitution; formatted code syntax to be more congruent with rest of codebase
1 parent 504d601 commit 2517bd9

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

imblearn/combine/_preprocess/_spider.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@
1010
from scipy import sparse
1111
from scipy import stats
1212

13-
from sklearn.utils import safe_indexing, safe_mask
13+
from sklearn.utils import safe_mask
14+
from sklearn.utils import _safe_indexing
1415

1516
from .base import BasePreprocessSampler
1617
from ...utils import check_neighbors_object
1718
from ...utils import Substitution
19+
from ..utils._docstring import _n_jobs_docstring
1820

19-
SEL_KIND = ('weak', 'relabel', 'strong')
21+
SEL_KIND = ("weak", "relabel", "strong")
2022

2123

2224
@Substitution(
23-
sampling_strategy=BasePreprocessSampler._sampling_strategy_docstring)
25+
sampling_strategy=BasePreprocessSampler._sampling_strategy_docstring,
26+
n_jobs=_n_jobs_docstring,
27+
)
2428
class SPIDER(BasePreprocessSampler):
2529
"""Perform filtering and over-sampling using Selective Pre-processing of
2630
Imbalanced Data (SPIDER) sampling approach for imbalanced datasets.
@@ -55,8 +59,7 @@ class SPIDER(BasePreprocessSampler):
5559
The number to add to amplified samples during if ``kind`` is
5660
``'strong'``. This has no effect otherwise.
5761
58-
n_jobs : int, optional (default=1)
59-
Number of threads to run the algorithm when it is possible.
62+
{n_jobs}
6063
6164
Notes
6265
-----
@@ -101,11 +104,11 @@ class SPIDER(BasePreprocessSampler):
101104

102105
def __init__(
103106
self,
104-
sampling_strategy='auto',
105-
kind='weak',
107+
sampling_strategy="auto",
108+
kind="weak",
106109
n_neighbors=3,
107110
additional_neighbors=2,
108-
n_jobs=1,
111+
n_jobs=None,
109112
):
110113
super().__init__(sampling_strategy=sampling_strategy)
111114
self.kind = kind
@@ -116,19 +119,20 @@ def __init__(
116119
def _validate_estimator(self):
117120
"""Create the necessary objects for SPIDER"""
118121
self.nn_ = check_neighbors_object(
119-
'n_neighbors', self.n_neighbors, additional_neighbor=1)
120-
self.nn_.set_params(**{'n_jobs': self.n_jobs})
122+
"n_neighbors", self.n_neighbors, additional_neighbor=1)
123+
self.nn_.set_params(**{"n_jobs": self.n_jobs})
121124

122125
if self.kind not in SEL_KIND:
123-
raise ValueError('The possible "kind" of algorithm are '
124-
'"weak", "relabel", and "strong".'
125-
'Got {} instead.'.format(self.kind))
126+
raise ValueError(
127+
'The possible "kind" of algorithm are "weak", "relabel",'
128+
' and "strong". Got {} instead.'.format(self.kind)
129+
)
126130

127131
if self.additional_neighbors < 1:
128-
raise ValueError('additional_neighbors must be at least 1.')
132+
raise ValueError("additional_neighbors must be at least 1.")
129133

130134
if not isinstance(self.additional_neighbors, Integral):
131-
raise TypeError('additional_neighbors must be an integer.')
135+
raise TypeError("additional_neighbors must be an integer.")
132136

133137
def _locate_neighbors(self, X, additional=False):
134138
"""Find nearest neighbors for samples.
@@ -249,22 +253,22 @@ def _fit_resample(self, X, y):
249253
discard_indices = np.flatnonzero(~is_class & ~is_safe)
250254

251255
class_noisy_indices = np.flatnonzero(is_class & ~is_safe)
252-
X_class_noisy = safe_indexing(X, class_noisy_indices)
256+
X_class_noisy = _safe_indexing(X, class_noisy_indices)
253257
y_class_noisy = y[class_noisy_indices]
254258

255-
if self.kind in ('weak', 'relabel'):
259+
if self.kind in ("weak", "relabel"):
256260
nn_indices = self._amplify(X_class_noisy, y_class_noisy)
257261

258-
if self.kind == 'relabel':
262+
if self.kind == "relabel":
259263
relabel_mask = np.isin(nn_indices, discard_indices)
260264
relabel_indices = np.unique(nn_indices[relabel_mask])
261265
self._y[relabel_indices] = class_sample
262266
discard_indices = np.setdiff1d(
263267
discard_indices, relabel_indices)
264268

265-
elif self.kind == 'strong':
269+
elif self.kind == "strong":
266270
class_safe_indices = np.flatnonzero(is_class & is_safe)
267-
X_class_safe = safe_indexing(X, class_safe_indices)
271+
X_class_safe = _safe_indexing(X, class_safe_indices)
268272
y_class_safe = y[class_safe_indices]
269273
self._amplify(X_class_safe, y_class_safe)
270274

imblearn/utils/_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def _sampling_strategy_dict(sampling_strategy, y, sampling_type):
290290
)
291291
)
292292
sampling_strategy_[class_sample] = n_samples
293-
elif sampling_type in ("clean-sampling", "preprocess-sampling":
293+
elif sampling_type in ("clean-sampling", "preprocess-sampling"):
294294
raise ValueError(
295295
"'sampling_strategy' as a dict for cleaning or preprocess "
296296
"methods is not supported. Please give a list of the classes "

0 commit comments

Comments
 (0)