Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e7e9eee

Browse filesBrowse files
committed
Application of suggestions : sooner check for valid strategy + interlacing of conditions to avoid repetitions
1 parent ea5f446 commit e7e9eee
Copy full SHA for e7e9eee

File tree

Expand file treeCollapse file tree

2 files changed

+43
-18
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+43
-18
lines changed

‎sklearn/preprocessing/_discretization.py

Copy file name to clipboardExpand all lines: sklearn/preprocessing/_discretization.py
+18-18Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ def fit(self, X, y=None, sample_weight=None):
210210

211211
n_samples, n_features = X.shape
212212

213+
valid_strategy = ("uniform", "quantile", "kmeans")
214+
if self.strategy not in valid_strategy:
215+
raise ValueError(
216+
"Valid options for 'strategy' are {}. "
217+
"Got strategy={!r} instead.".format(valid_strategy, self.strategy)
218+
)
219+
213220
if self.strategy == "quantile" and self.subsample is not None:
214221
if self.subsample == "warn":
215222
if n_samples > 2e5:
@@ -230,18 +237,17 @@ def fit(self, X, y=None, sample_weight=None):
230237
n_samples, size=self.subsample, replace=False
231238
)
232239
X = _safe_indexing(X, subsample_idx)
233-
elif self.strategy != "quantile" and sample_weight is not None:
234-
raise ValueError(
235-
"`sample_weight` was provided but it can be only used with"
236-
f"strategy='quantile'. Got strategy={self.strategy!r} instead."
237-
)
238-
elif self.strategy != "quantile" and isinstance(
239-
self.subsample, numbers.Integral
240-
):
241-
raise ValueError(
242-
f"Invalid parameter for `strategy`: {self.strategy}. "
243-
'`subsample` must be used with `strategy="quantile"`.'
244-
)
240+
elif self.strategy != "quantile":
241+
if isinstance(self.subsample, numbers.Integral):
242+
raise ValueError(
243+
f"Invalid parameter for `strategy`: {self.strategy}. "
244+
'`subsample` must be used with `strategy="quantile"`.'
245+
)
246+
if sample_weight is not None:
247+
raise ValueError(
248+
"`sample_weight` was provided but it can be only used with"
249+
f"strategy='quantile'. Got strategy={self.strategy!r} instead."
250+
)
245251

246252
valid_encode = ("onehot", "onehot-dense", "ordinal")
247253
if self.encode not in valid_encode:
@@ -250,12 +256,6 @@ def fit(self, X, y=None, sample_weight=None):
250256
valid_encode, self.encode
251257
)
252258
)
253-
valid_strategy = ("uniform", "quantile", "kmeans")
254-
if self.strategy not in valid_strategy:
255-
raise ValueError(
256-
"Valid options for 'strategy' are {}. "
257-
"Got strategy={!r} instead.".format(valid_strategy, self.strategy)
258-
)
259259

260260
n_features = X.shape[1]
261261
n_bins = self._validate_n_bins(n_features)

‎sklearn/preprocessing/tests/test_discretization.py

Copy file name to clipboardExpand all lines: sklearn/preprocessing/tests/test_discretization.py
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [2, 2, 2, 2]],
2727
[1, 1, 2, 1],
2828
),
29+
(
30+
"quantile",
31+
[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [2, 2, 2, 2]],
32+
[1, 1, 1, 1],
33+
),
34+
(
35+
"quantile",
36+
[[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]],
37+
[0, 1, 1, 1],
38+
),
2939
],
3040
)
3141
def test_fit_transform(strategy, expected, sample_weight):
@@ -107,6 +117,21 @@ def test_invalid_n_bins_array():
107117
[[0, 0, 0, 0], [0, 1, 1, 1], [1, 2, 2, 2], [1, 2, 2, 2]],
108118
[1, 1, 3, 1],
109119
),
120+
(
121+
"quantile",
122+
[[0, 0, 0, 0], [1, 1, 1, 1], [1, 2, 2, 2], [1, 2, 2, 2]],
123+
[1, 1, 1, 1],
124+
),
125+
(
126+
"quantile",
127+
[[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]],
128+
[0, 1, 3, 1],
129+
),
130+
(
131+
"quantile",
132+
[[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]],
133+
[0, 1, 3, 1],
134+
),
110135
],
111136
)
112137
def test_fit_transform_n_bins_array(strategy, expected, sample_weight):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.