Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

VirenPassi
Copy link
Contributor

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.

…mes usage

Add docstrings suggesting validate_data(skip_check_array=True) as the preferred option.
Add inline comment to explain usage in FunctionTransformer.
Copy link

github-actions bot commented Jun 18, 2025

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 44be546. Link to the linter CI: here

Comment on lines 185 to 186
# Usage of _check_n_features and _check_feature_names here aligns with
# validate_data(..., skip_check_array=True), which avoids full validation.
Copy link
Member

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?

Copy link
Contributor Author

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!

Copy link
Member

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.

Copy link
Contributor Author

@VirenPassi VirenPassi Jun 19, 2025

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!

@adrinjalali adrinjalali changed the title Improve documentation for _check_n_features and _check_feature_names (#30389) DOC improve doc for _check_n_features and _check_feature_names and fix their usages Jun 19, 2025
_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)
Copy link
Member

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.

Copy link
Contributor Author

@VirenPassi VirenPassi Jun 20, 2025

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!

Copy link
Member

@jeremiedbb jeremiedbb left a 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",
Copy link
Member

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.

Suggested change
dtype="numeric",

reset=True,
accept_sparse=self.accept_sparse,
dtype="numeric",
ensure_all_finite="allow-nan",
Copy link
Member

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,
Copy link
Member

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

Suggested change
ensure_2d=True,
ensure_2d=self.validate,

Comment on lines -2711 to -2715
.. 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`.
Copy link
Member

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The estimator to check or set features name for.
The estimator to check or set feature names for.

Comment on lines +2711 to +2712
This is typically used when `validate=False` during fitting, to still
record the feature names.
Copy link
Member

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.

Suggested change
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.
Copy link
Member

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 ?

Suggested change
"""Check or set the `feature_names_in_` attribute for an estimator.
"""Set or check the `feature_names_in_` attribute of an estimator.

Comment on lines -2729 to -2732
.. 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`.
Copy link
Member

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

Comment on lines -2821 to -2824
.. 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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants