Skip to content

Commit c381750

Browse files
authored
API feature_selection's constructor params -> kwonly (scikit-learn#16867)
1 parent 0447dd9 commit c381750

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

sklearn/feature_selection/_from_model.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from ..exceptions import NotFittedError
1212
from ..utils.metaestimators import if_delegate_has_method
13+
from ..utils.validation import _deprecate_positional_args
1314

1415

1516
def _get_feature_importances(estimator, norm_order=1):
@@ -157,7 +158,8 @@ class SelectFromModel(MetaEstimatorMixin, SelectorMixin, BaseEstimator):
157158
[-0.48],
158159
[ 1.48]])
159160
"""
160-
def __init__(self, estimator, threshold=None, prefit=False,
161+
@_deprecate_positional_args
162+
def __init__(self, estimator, *, threshold=None, prefit=False,
161163
norm_order=1, max_features=None):
162164
self.estimator = estimator
163165
self.threshold = threshold

sklearn/feature_selection/_rfe.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ..utils.metaestimators import if_delegate_has_method
1414
from ..utils.metaestimators import _safe_split
1515
from ..utils.validation import check_is_fitted
16+
from ..utils.validation import _deprecate_positional_args
1617
from ..base import BaseEstimator
1718
from ..base import MetaEstimatorMixin
1819
from ..base import clone
@@ -95,7 +96,7 @@ class RFE(SelectorMixin, MetaEstimatorMixin, BaseEstimator):
9596
>>> from sklearn.svm import SVR
9697
>>> X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
9798
>>> estimator = SVR(kernel="linear")
98-
>>> selector = RFE(estimator, 5, step=1)
99+
>>> selector = RFE(estimator, n_features_to_select=5, step=1)
99100
>>> selector = selector.fit(X, y)
100101
>>> selector.support_
101102
array([ True, True, True, True, True, False, False, False, False,
@@ -119,7 +120,8 @@ class RFE(SelectorMixin, MetaEstimatorMixin, BaseEstimator):
119120
for cancer classification using support vector machines",
120121
Mach. Learn., 46(1-3), 389--422, 2002.
121122
"""
122-
def __init__(self, estimator, n_features_to_select=None, step=1,
123+
@_deprecate_positional_args
124+
def __init__(self, estimator, *, n_features_to_select=None, step=1,
123125
verbose=0):
124126
self.estimator = estimator
125127
self.n_features_to_select = n_features_to_select
@@ -464,7 +466,8 @@ class RFECV(RFE):
464466
for cancer classification using support vector machines",
465467
Mach. Learn., 46(1-3), 389--422, 2002.
466468
"""
467-
def __init__(self, estimator, step=1, min_features_to_select=1, cv=None,
469+
@_deprecate_positional_args
470+
def __init__(self, estimator, *, step=1, min_features_to_select=1, cv=None,
468471
scoring=None, verbose=0, n_jobs=None):
469472
self.estimator = estimator
470473
self.step = step

sklearn/feature_selection/_univariate_selection.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
safe_mask)
1818
from ..utils.extmath import safe_sparse_dot, row_norms
1919
from ..utils.validation import check_is_fitted
20+
from ..utils.validation import _deprecate_positional_args
2021
from ._base import SelectorMixin
2122

2223

@@ -419,9 +420,9 @@ class SelectPercentile(_BaseFilter):
419420
SelectFwe: Select features based on family-wise error rate.
420421
GenericUnivariateSelect: Univariate feature selector with configurable mode.
421422
"""
422-
423-
def __init__(self, score_func=f_classif, percentile=10):
424-
super().__init__(score_func)
423+
@_deprecate_positional_args
424+
def __init__(self, score_func=f_classif, *, percentile=10):
425+
super().__init__(score_func=score_func)
425426
self.percentile = percentile
426427

427428
def _check_params(self, X, y):
@@ -503,9 +504,9 @@ class SelectKBest(_BaseFilter):
503504
SelectFwe: Select features based on family-wise error rate.
504505
GenericUnivariateSelect: Univariate feature selector with configurable mode.
505506
"""
506-
507-
def __init__(self, score_func=f_classif, k=10):
508-
super().__init__(score_func)
507+
@_deprecate_positional_args
508+
def __init__(self, score_func=f_classif, *, k=10):
509+
super().__init__(score_func=score_func)
509510
self.k = k
510511

511512
def _check_params(self, X, y):
@@ -582,9 +583,9 @@ class SelectFpr(_BaseFilter):
582583
SelectFwe: Select features based on family-wise error rate.
583584
GenericUnivariateSelect: Univariate feature selector with configurable mode.
584585
"""
585-
586-
def __init__(self, score_func=f_classif, alpha=5e-2):
587-
super().__init__(score_func)
586+
@_deprecate_positional_args
587+
def __init__(self, score_func=f_classif, *, alpha=5e-2):
588+
super().__init__(score_func=score_func)
588589
self.alpha = alpha
589590

590591
def _get_support_mask(self):
@@ -648,9 +649,9 @@ class SelectFdr(_BaseFilter):
648649
SelectFwe: Select features based on family-wise error rate.
649650
GenericUnivariateSelect: Univariate feature selector with configurable mode.
650651
"""
651-
652-
def __init__(self, score_func=f_classif, alpha=5e-2):
653-
super().__init__(score_func)
652+
@_deprecate_positional_args
653+
def __init__(self, score_func=f_classif, *, alpha=5e-2):
654+
super().__init__(score_func=score_func)
654655
self.alpha = alpha
655656

656657
def _get_support_mask(self):
@@ -711,9 +712,9 @@ class SelectFwe(_BaseFilter):
711712
SelectFdr: Select features based on an estimated false discovery rate.
712713
GenericUnivariateSelect: Univariate feature selector with configurable mode.
713714
"""
714-
715-
def __init__(self, score_func=f_classif, alpha=5e-2):
716-
super().__init__(score_func)
715+
@_deprecate_positional_args
716+
def __init__(self, score_func=f_classif, *, alpha=5e-2):
717+
super().__init__(score_func=score_func)
717718
self.alpha = alpha
718719

719720
def _get_support_mask(self):
@@ -761,7 +762,7 @@ class GenericUnivariateSelect(_BaseFilter):
761762
>>> X, y = load_breast_cancer(return_X_y=True)
762763
>>> X.shape
763764
(569, 30)
764-
>>> transformer = GenericUnivariateSelect(chi2, 'k_best', param=20)
765+
>>> transformer = GenericUnivariateSelect(chi2, mode='k_best', param=20)
765766
>>> X_new = transformer.fit_transform(X, y)
766767
>>> X_new.shape
767768
(569, 20)
@@ -786,8 +787,9 @@ class GenericUnivariateSelect(_BaseFilter):
786787
'fdr': SelectFdr,
787788
'fwe': SelectFwe}
788789

789-
def __init__(self, score_func=f_classif, mode='percentile', param=1e-5):
790-
super().__init__(score_func)
790+
@_deprecate_positional_args
791+
def __init__(self, score_func=f_classif, *, mode='percentile', param=1e-5):
792+
super().__init__(score_func=score_func)
791793
self.mode = mode
792794
self.param = param
793795

sklearn/feature_selection/tests/test_feature_select.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def test_nans():
552552
X = [[0, 1, 0], [0, -1, -1], [0, .5, .5]]
553553
y = [1, 0, 1]
554554

555-
for select in (SelectKBest(f_classif, 2),
555+
for select in (SelectKBest(f_classif, k=2),
556556
SelectPercentile(f_classif, percentile=67)):
557557
ignore_warnings(select.fit)(X, y)
558558
assert_array_equal(select.get_support(indices=True), np.array([1, 2]))

0 commit comments

Comments
 (0)