-
-
Notifications
You must be signed in to change notification settings - Fork 26k
DOC improve doc for _check_n_features
and _check_feature_names
and fix their usages
#31585
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
base: main
Are you sure you want to change the base?
Conversation
…mes usage Add docstrings suggesting validate_data(skip_check_array=True) as the preferred option. Add inline comment to explain usage in FunctionTransformer.
# Usage of _check_n_features and _check_feature_names here aligns with | ||
# validate_data(..., skip_check_array=True), which avoids full validation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we do validate_data(..., skip_check_array=True)
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! You're absolutely right — in principle, validate_data(..., skip_check_array=True)
could replace these internal calls.
In this PR, I kept the original usage to avoid changing existing logic, since the primary goal was documentation improvement and clarification. If preferred, I'm happy to open a follow-up PR that refactors the usage to use validate_data()
directly and confirms everything still behaves correctly.
Let me know how you'd like to proceed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be nice to change the logic here in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion @adrinjalali! I’ve updated _check_input to use validate_data(..., skip_check_array=True) directly as you recommended, replacing the internal checks.
I also tested it locally using Python 3.10.11 — the file test_function_transformer.py passed without any issues. Let me know if anything else looks off!
_check_n_features
and _check_feature_names
and fix their usages
_check_n_features(self, X, reset=reset) | ||
_check_feature_names(self, X, reset=reset) | ||
# Use validate_data to perform shape and feature name checks | ||
return validate_data(self, X, reset=reset, skip_check_array=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this whole method (_check_input
) can be removed and replaced with a validate_data(..., skip_check_array=self.validate)
kinda call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! Just wanted to note that a few CI jobs are failing due to the stricter validation added by replacing _check_input() with validate_data(...).
Specifically, test_transform_target_regressor_1d_transformer now raises a ValueError because it passes an empty input X, and validate_data correctly catches that.
Let me know if you'd like me to update the test or handle it a different way — happy to make the changes!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @VirenPassi. Here are some comments
X, | ||
reset=True, | ||
accept_sparse=self.accept_sparse, | ||
dtype="numeric", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the default value in check_array
so no need to repeat it here.
dtype="numeric", |
reset=True, | ||
accept_sparse=self.accept_sparse, | ||
dtype="numeric", | ||
ensure_all_finite="allow-nan", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you set it to allow-nan
? The default is None
which corresponds to True
. Note that if validate=False, check_array is skipped so ensure_all_finite is irrelevant.
accept_sparse=self.accept_sparse, | ||
dtype="numeric", | ||
ensure_all_finite="allow-nan", | ||
ensure_2d=True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ensure_2d
is used in validate_data
even if check_array
is skipped. So we should condition it to the value of validate
as well. This should fix the CI failures
ensure_2d=True, | |
ensure_2d=self.validate, |
.. versionadded:: 1.0 | ||
This is typically used when `validate=False` during fitting, to still | ||
record the feature names. | ||
|
||
.. versionchanged:: 1.6 | ||
Moved from :class:`~sklearn.base.BaseEstimator` to | ||
:mod:`sklearn.utils.validation`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you remove this ? It's still usefull imo
|
||
Parameters | ||
---------- | ||
estimator : estimator instance | ||
The estimator to validate the input for. | ||
The estimator to check or set features name for. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The estimator to check or set features name for. | |
The estimator to check or set feature names for. |
This is typically used when `validate=False` during fitting, to still | ||
record the feature names. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has nothing to do with validate
. It always happens unless in very specific cases. and validate
is not a common parameter of estimators, only a few of them have it). So I think this sentence is misleading and I would not include it.
This is typically used when `validate=False` during fitting, to still | |
record the feature names. |
@@ -2706,30 +2706,27 @@ def _to_object_array(sequence): | |||
|
|||
|
|||
def _check_feature_names(estimator, X, *, reset): | |||
"""Set or check the `feature_names_in_` attribute of an estimator. | |||
"""Check or set the `feature_names_in_` attribute for an estimator. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think of
was perfectly fine. And why change the order of check
and set
?
"""Check or set the `feature_names_in_` attribute for an estimator. | |
"""Set or check the `feature_names_in_` attribute of an estimator. |
.. note:: | ||
It is recommended to call `reset=True` in `fit` and in the first | ||
call to `partial_fit`. All other methods that validate `X` | ||
should set `reset=False`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This not is important. I'd keep it
.. note:: | ||
It is recommended to call reset=True in `fit` and in the first | ||
call to `partial_fit`. All other methods that validate `X` | ||
should set `reset=False`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
This PR improves the documentation for _check_n_features and _check_feature_names by:
Adding clear docstrings to guide users toward using validate_data(skip_check_array=True) as the preferred public interface.
Including a clarifying comment in FunctionTransformer._check_input explaining that the use of these internal functions aligns with validate_data(..., skip_check_array=True) and is appropriate in this context.