2
2
This file contains preprocessing tools based on polynomials.
3
3
"""
4
4
import collections
5
- import numbers
6
5
from numbers import Integral
7
6
from itertools import chain , combinations
8
7
from itertools import combinations_with_replacement as combinations_w_r
16
15
from ..utils import check_array
17
16
from ..utils .deprecation import deprecated
18
17
from ..utils .validation import check_is_fitted , FLOAT_DTYPES , _check_sample_weight
19
- from ..utils ._param_validation import Interval , StrOptions
20
18
from ..utils .validation import _check_feature_names_in
19
+ from ..utils ._param_validation import Interval , StrOptions
21
20
from ..utils .stats import _weighted_percentile
22
21
23
22
from ._csr_polynomial_expansion import _csr_polynomial_expansion
@@ -131,6 +130,13 @@ class PolynomialFeatures(TransformerMixin, BaseEstimator):
131
130
[ 1., 4., 5., 20.]])
132
131
"""
133
132
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
+
134
140
def __init__ (
135
141
self , degree = 2 , * , interaction_only = False , include_bias = True , order = "C"
136
142
):
@@ -286,14 +292,11 @@ def fit(self, X, y=None):
286
292
self : object
287
293
Fitted transformer.
288
294
"""
295
+ self ._validate_params ()
289
296
_ , n_features = self ._validate_data (X , accept_sparse = True ).shape
290
297
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 :
297
300
raise ValueError (
298
301
"Setting degree to zero and include_bias to False would result in"
299
302
" an empty output array."
@@ -306,8 +309,8 @@ def fit(self, X, y=None):
306
309
):
307
310
self ._min_degree , self ._max_degree = self .degree
308
311
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 )
311
314
and self ._min_degree >= 0
312
315
and self ._min_degree <= self ._max_degree
313
316
):
@@ -319,7 +322,7 @@ def fit(self, X, y=None):
319
322
)
320
323
elif self ._max_degree == 0 and not self .include_bias :
321
324
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"
323
326
" False would result in an empty output array."
324
327
)
325
328
else :
0 commit comments