8000 MAINT Add parameter validation to `PolynomialFeatures` (#24214) · scikit-learn/scikit-learn@ca4e076 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca4e076

Browse files
authored
MAINT Add parameter validation to PolynomialFeatures (#24214)
1 parent 30b1f35 commit ca4e076

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

sklearn/preprocessing/_polynomial.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
This file contains preprocessing tools based on polynomials.
33
"""
44
import collections
5-
import numbers
65
from numbers import Integral
76
from itertools import chain, combinations
87
from itertools import combinations_with_replacement as combinations_w_r
@@ -16,8 +15,8 @@
1615
from ..utils import check_array
1716
from ..utils.deprecation import deprecated
1817
from ..utils.validation import check_is_fitted, FLOAT_DTYPES, _check_sample_weight
19-
from ..utils._param_validation import Interval, StrOptions
2018
from ..utils.validation import _check_feature_names_in
19+
from ..utils._param_validation import Interval, StrOptions
2120
from ..utils.stats import _weighted_percentile
2221

2322
from ._csr_polynomial_expansion import _csr_polynomial_expansion
@@ -131,6 +130,13 @@ class PolynomialFeatures(TransformerMixin, BaseEstimator):
131130
[ 1., 4., 5., 20.]])
132131
"""
133132

133+
_parameter_constraints = {
134+
"degree": [Interval(Integral, 0, None, closed="left"), "array-like"],
135+
"interaction_only": ["boolean"],
136+
"include_bias": ["boolean"],
137+
"order": [StrOptions({"C", "F"})],
138+
}
139+
134140
def __init__(
135141
self, degree=2, *, interaction_only=False, include_bias=True, order="C"
136142
):
@@ -286,14 +292,11 @@ def fit(self, X, y=None):
286292
self : object
287293
Fitted transformer.
288294
"""
295+
self._validate_params()
289296
_, n_features = self._validate_data(X, accept_sparse=True).shape
290297

291-
if isinstance(self.degree, numbers.Integral):
292-
if self.degree < 0:
293-
raise ValueError(
294-
f"degree must be a non-negative integer, got {self.degree}."
295-
)
296-
elif self.degree == 0 and not self.include_bias:
298+
if isinstance(self.degree, Integral):
299+
if self.degree == 0 and not self.include_bias:
297300
raise ValueError(
298301
"Setting degree to zero and include_bias to False would result in"
299302
" an empty output array."
@@ -306,8 +309,8 @@ def fit(self, X, y=None):
306309
):
307310
self._min_degree, self._max_degree = self.degree
308311
if not (
309-
isinstance(self._min_degree, numbers.Integral)
310-
and isinstance(self._max_degree, numbers.Integral)
312+
isinstance(self._min_degree, Integral)
313+
and isinstance(self._max_degree, Integral)
311314
and self._min_degree >= 0
312315
and self._min_degree <= self._max_degree
313316
):
@@ -319,7 +322,7 @@ def fit(self, X, y=None):
319322
)
320323
elif self._max_degree == 0 and not self.include_bias:
321324
raise ValueError(
322-
"Setting both min_deree and max_degree to zero and include_bias to"
325+
"Setting both min_degree and max_degree to zero and include_bias to"
323326
" False would result in an empty output array."
324327
)
325328
else:

sklearn/preprocessing/tests/test_polynomial.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,10 @@ def test_spline_transformer_n_features_out(n_knots, include_bias, degree):
366366
@pytest.mark.parametrize(
367367
"params, err_msg",
368368
[
369-
({"degree": -1}, "degree must be a non-negative integer"),
370-
({"degree": 2.5}, "degree must be a non-negative int or tuple"),
371-
({"degree": "12"}, r"degree=\(min_degree, max_degree\) must"),
372-
({"degree": "string"}, "degree must be a non-negative int or tuple"),
373369
({"degree": (-1, 2)}, r"degree=\(min_degree, max_degree\) must"),
374370
({"degree": (0, 1.5)}, r"degree=\(min_degree, max_degree\) must"),
375371
({"degree": (3, 2)}, r"degree=\(min_degree, max_degree\) must"),
372+
({"degree": (1, 2, 3)}, r"int or tuple \(min_degree, max_degree\)"),
376373
],
377374
)
378375
def test_polynomial_features_input_validation(params, err_msg):
@@ -816,7 +813,7 @@ def test_polynomial_features_behaviour_on_zero_degree():
816813

817814
poly = PolynomialFeatures(degree=(0, 0), include_bias=False)
818815
err_msg = (
819-
"Setting both min_deree and max_degree to zero and include_bias to"
816+
"Setting both min_degree and max_degree to zero and include_bias to"
820817
" False would result in an empty output array."
821818
)
822819
with pytest.raises(ValueError, match=err_msg):

sklearn/tests/test_common.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,6 @@ def test_estimators_do_not_raise_errors_in_init_or_set_params(Estimator):
495495
"OneVsRestClassifier",
496496
"PatchExtractor",
497497
"PolynomialCountSketch",
498-
"PolynomialFeatures",
499498
"RANSACRegressor",
500499
"RBFSampler",
501500
"RFE",

0 commit comments

Comments
 (0)
0