Skip to content

MAINT validate parameter in VarianceThreshold #23581

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 7 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions sklearn/feature_selection/_variance_threshold.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Author: Lars Buitinck
# License: 3-clause BSD
from numbers import Real

import numpy as np
from ..base import BaseEstimator
from ._base import SelectorMixin
from ..utils.sparsefuncs import mean_variance_axis, min_max_axis
from ..utils.validation import check_is_fitted
from ..utils._param_validation import Interval


class VarianceThreshold(SelectorMixin, BaseEstimator):
Expand Down Expand Up @@ -67,6 +69,8 @@ class VarianceThreshold(SelectorMixin, BaseEstimator):
[1, 1]])
"""

_parameter_constraints = {"threshold": [Interval(Real, 0, None, closed="left")]}

def __init__(self, threshold=0.0):
self.threshold = threshold

Expand All @@ -88,6 +92,7 @@ def fit(self, X, y=None):
self : object
Returns the instance itself.
"""
self._validate_params()
X = self._validate_data(
X,
accept_sparse=("csr", "csc"),
Expand All @@ -110,8 +115,6 @@ def fit(self, X, y=None):
# for constant features
compare_arr = np.array([self.variances_, peak_to_peaks])
self.variances_ = np.nanmin(compare_arr, axis=0)
elif self.threshold < 0.0:
raise ValueError(f"Threshold must be non-negative. Got: {self.threshold}")

if np.all(~np.isfinite(self.variances_) | (self.variances_ <= self.threshold)):
msg = "No feature in X meets the variance threshold {0:.5f}"
Expand Down
9 changes: 0 additions & 9 deletions sklearn/feature_selection/tests/test_variance_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ def test_variance_threshold():
assert (len(data), 1) == X.shape


@pytest.mark.parametrize("X", [data, csr_matrix(data)])
def test_variance_negative(X):
"""Test VarianceThreshold with negative variance."""
var_threshold = VarianceThreshold(threshold=-1.0)
msg = r"^Threshold must be non-negative. Got: -1.0$"
with pytest.raises(ValueError, match=msg):
var_threshold.fit(X)


@pytest.mark.skipif(
np.var(data2) == 0,
reason=(
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 @@ -619,7 +619,6 @@ def test_estimators_do_not_raise_errors_in_init_or_set_params(Estimator):
"TransformedTargetRegressor",
"TruncatedSVD",
"TweedieRegressor",
"VarianceThreshold",
"VotingClassifier",
"VotingRegressor",
]
Expand Down