8000 MAINT Add parameter validation to `PolynomialFeatures` by stefmolin · Pull Request #24214 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

MAINT Add parameter validation to PolynomialFeatures #24214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

< 8000 /div>
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions sklearn/preprocessing/_polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
This file contains preprocessing tools based on polynomials.
"""
import collections
import numbers
from numbers import Integral
from itertools import chain, combinations
from itertools import combinations_with_replacement as combinations_w_r
Expand All @@ -16,8 +15,8 @@
from ..utils import check_array
from ..utils.deprecation import deprecated
from ..utils.validation import check_is_fitted, FLOAT_DTYPES, _check_sample_weight
from ..utils._param_validation import Interval, StrOptions
from ..utils.validation import _check_feature_names_in
from ..utils._param_validation import Interval, StrOptions
from ..utils.stats import _weighted_percentile

from ._csr_polynomial_expansion import _csr_polynomial_expansion
Expand Down Expand Up @@ -131,6 +130,13 @@ class PolynomialFeatures(TransformerMixin, BaseEstimator):
[ 1., 4., 5., 20.]])
"""

_parameter_constraints = {
"degree": [Interval(Integral, 0, None, closed="left"), "array-like"],
"interaction_only": ["boolean"],
"include_bias": ["boolean"],
"order": [StrOptions({"C", "F"})],
}

def __init__(
self, degree=2, *, interaction_only=False, include_bias=True, order="C"
):
Expand Down Expand Up @@ -286,14 +292,11 @@ def fit(self, X, y=None):
self : object
Fitted transformer.
"""
self._validate_params()
_, n_features = self._validate_data(X, accept_sparse=True).shape

if isinstance(self.degree, numbers.Integral):
if self.degree < 0:
raise ValueError(
f"degree must be a non-negative integer, got {self.degree}."
)
elif self.degree == 0 and not self.include_bias:
if isinstance(self.degree, Integral):
if self.degree == 0 and not self.include_bias:
raise ValueError(
"Setting degree to zero and include_bias to False would result in"
" an empty output array."
Expand All @@ -306,8 +309,8 @@ def fit(self, X, y=None):
):
self._min_degree, self._max_degree = self.degree
if not (
isinstance(self._min_degree, numbers.Integral)
and isinstance(self._max_degree, numbers.Integral)
isinstance(self._min_degree, Integral)
and isinstance(self._max_degree, Integral)
and self._min_degree >= 0
and self._min_degree <= self._max_degree
):
Expand All @@ -319,7 +322,7 @@ def fit(self, X, y=None):
)
elif self._max_degree == 0 and not self.include_bias:
raise ValueError(
"Setting both min_deree and max_degree to zero and include_bias to"
"Setting both min_degree and max_degree to zero and include_bias to"
" False would result in an empty output array."
)
else:
Expand Down
7 changes: 2 additions & 5 deletions sklearn/preprocessing/tests/test_polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,10 @@ def test_spline_transformer_n_features_out(n_knots, include_bias, degree):
@pytest.mark.parametrize(
"params, err_msg",
[
({"degree": -1}, "degree must be a non-negative integer"),
({"degree": 2.5}, "degree must be a non-negative int or tuple"),
({"degree": "12"}, r"degree=\(min_degree, max_degree\) must"),
({"degree": "string"}, "degree must be a non-negative int or tuple"),
({"degree": (-1, 2)}, r"degree=\(min_degree, max_degree\) must"),
({"degree": (0, 1.5)}, r"degree=\(min_degree, max_degree\) must"),
({"degree": (3, 2)}, r"degree=\(min_degree, max_degree\) must"),
({"degree": (1, 2, 3)}, r"int or tuple \(min_degree, max_degree\)"),
],
)
def test_polynomial_features_input_validation(params, err_msg):
Expand Down Expand Up @@ -816,7 +813,7 @@ def test_polynomial_features_behaviour_on_zero_degree():

poly = PolynomialFeatures(degree=(0, 0), include_bias=False)
err_msg = (
"Setting both min_deree and max_degree to zero and include_bias to"
"Setting both min_degree and max_degree to zero and include_bias to"
" False would result in an empty output array."
)
with pytest.raises(ValueError, match=err_msg):
Expand Down
1 change: 0 additions & 1 deletion sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ def test_estimators_do_not_raise_errors_in_init_or_set_params(Estimator):
"OneVsRestClassifier",
"PatchExtractor",
"PolynomialCountSketch",
"PolynomialFeatures",
"RANSACRegressor",
"RBFSampler",
"RFE",
Expand Down
0