Skip to content

Commit a1f5952

Browse files
DOC improve doc for _check_n_features and _check_feature_names (scikit-learn#31585)
Co-authored-by: Jérémie du Boisberranger <jeremie@probabl.ai>
1 parent 2ef4853 commit a1f5952

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

sklearn/preprocessing/_function_transformer.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
from ..utils.metaestimators import available_if
1717
from ..utils.validation import (
1818
_allclose_dense_sparse,
19-
_check_feature_names,
2019
_check_feature_names_in,
21-
_check_n_features,
2220
_get_feature_names,
2321
_is_pandas_df,
2422
_is_polars_df,
@@ -178,17 +176,6 @@ def __init__(
178176
self.kw_args = kw_args
179177
self.inv_kw_args = inv_kw_args
180178

181-
def _check_input(self, X, *, reset):
182-
if self.validate:
183-
return validate_data(self, X, accept_sparse=self.accept_sparse, reset=reset)
184-
elif reset:
185-
# Set feature_names_in_ and n_features_in_ even if validate=False
186-
# We run this only when reset==True to store the attributes but not
187-
# validate them, because validate=False
188-
_check_n_features(self, X, reset=reset)
189-
_check_feature_names(self, X, reset=reset)
190-
return X
191-
192179
def _check_inverse_transform(self, X):
193180
"""Check that func and inverse_func are the inverse."""
194181
idx_selected = slice(None, None, max(1, X.shape[0] // 100))
@@ -240,7 +227,13 @@ def fit(self, X, y=None):
240227
self : object
241228
FunctionTransformer class instance.
242229
"""
243-
X = self._check_input(X, reset=True)
230+
X = validate_data(
231+
self,
232+
X,
233+
reset=True,
234+
accept_sparse=self.accept_sparse,
235+
skip_check_array=not self.validate,
236+
)
244237
if self.check_inverse and not (self.func is None or self.inverse_func is None):
245238
self._check_inverse_transform(X)
246239
return self
@@ -259,7 +252,9 @@ def transform(self, X):
259252
X_out : array-like, shape (n_samples, n_features)
260253
Transformed input.
261254
"""
262-
X = self._check_input(X, reset=False)
255+
if self.validate:
256+
X = validate_data(self, X, reset=False, accept_sparse=self.accept_sparse)
257+
263258
out = self._transform(X, func=self.func, kw_args=self.kw_args)
264259
output_config = _get_output_config("transform", self)["dense"]
265260

sklearn/utils/validation.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,10 @@ def _check_feature_names(estimator, X, *, reset):
27232723
Moved from :class:`~sklearn.base.BaseEstimator` to
27242724
:mod:`sklearn.utils.validation`.
27252725
2726+
.. note::
2727+
To only check feature names without conducting a full data validation, prefer
2728+
using `validate_data(..., skip_check_array=True)` if possible.
2729+
27262730
Parameters
27272731
----------
27282732
estimator : estimator instance
@@ -2733,8 +2737,10 @@ def _check_feature_names(estimator, X, *, reset):
27332737
27342738
reset : bool
27352739
Whether to reset the `feature_names_in_` attribute.
2740+
If True, resets the `feature_names_in_` attribute as inferred from `X`.
27362741
If False, the input will be checked for consistency with
27372742
feature names of data provided when reset was last True.
2743+
27382744
.. note::
27392745
It is recommended to call `reset=True` in `fit` and in the first
27402746
call to `partial_fit`. All other methods that validate `X`
@@ -2810,6 +2816,10 @@ def add_names(names):
28102816
def _check_n_features(estimator, X, reset):
28112817
"""Set the `n_features_in_` attribute, or check against it on an estimator.
28122818
2819+
.. note::
2820+
To only check n_features without conducting a full data validation, prefer
2821+
using `validate_data(..., skip_check_array=True)` if possible.
2822+
28132823
.. versionchanged:: 1.6
28142824
Moved from :class:`~sklearn.base.BaseEstimator` to
28152825
:mod:`~sklearn.utils.validation`.
@@ -2823,12 +2833,14 @@ def _check_n_features(estimator, X, reset):
28232833
The input samples.
28242834
28252835
reset : bool
2836+
Whether to reset the `n_features_in_` attribute.
28262837
If True, the `n_features_in_` attribute is set to `X.shape[1]`.
28272838
If False and the attribute exists, then check that it is equal to
28282839
`X.shape[1]`. If False and the attribute does *not* exist, then
28292840
the check is skipped.
2841+
28302842
.. note::
2831-
It is recommended to call reset=True in `fit` and in the first
2843+
It is recommended to call `reset=True` in `fit` and in the first
28322844
call to `partial_fit`. All other methods that validate `X`
28332845
should set `reset=False`.
28342846
"""

0 commit comments

Comments
 (0)