Repository navigation
FEAT Preserve DataFrame inputs and feature names in RFE and RFECV - #34789
Conversation
|
Thank you for opening your first pull request to scikit-learn! 🎉 To help get your contribution reviewed, please make sure that:
|
Previously, RFE's inference methods (predict, score, decision_function, etc.) called `self.transform(X)`. This inherently drops the Pandas DataFrame wrapper and returns a NumPy array unless explicitly overridden by `set_output(transform='pandas')`.
Since `RFE._fit` was updated to preserve the Pandas DataFrame, the underlying estimator was trained with feature names. When `predict` later passed a stripped NumPy array to the underlying estimator, it caused a UserWarning ('X does not have valid feature names, but estimator was fitted with feature names').
This commit introduces an internal `_transform_for_estimator(X)` helper. It validates and sub-indexes the DataFrame while retaining the Pandas container, ensuring the underlying estimator receives consistent data structures during both fit and inference.
|
Thank you for the PR @Kaynup! We are currently short on reviewers, maybe @Gabriel-Kissin or @cleboo could help with reviewing this, as they were involved in the issue? |
ogrisel
left a comment
There was a problem hiding this comment.
Thanks for the PR. Here is a pass of review.
- Set skip_check_array=True across RFE and RFECV - Remove redundant validation parameters - Unify and parametrize unit tests for RFE and RFECV across pandas and polars
- Use is_df_or_series from sklearn.utils._dataframe to preserve any DataFrame container (pandas, polars, pyarrow) - Convert plain Python lists to ndarrays for 2D slicing compatibility - Unify and parametrize unit tests for RFE and RFECV across pandas and polars using clone
I tested setting
I did the rest of the changes as you suggested |
ogrisel
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the follow-up.
Reference Issues/PRs
Fixes #28629
What does this implement/fix? Explain your changes.
Currently,
RFE._fitandRFECV.fitcoerce all input data to dense NumPy float arrays, which drops pandas DataFrame container information, column names, and categorical dtypes (such ascategorycolumns intended for estimators likeHistGradientBoostingClassifier(categorical_features="from_dtype")or XGBoost). Furthermore, hardcoded array slicingX[:, features]causes errors on DataFrames.This PR implements the approach agreed upon in the issue discussion:
RFE._fitandRFECV.fitdata validation withSelectorMixin.transformby preserving DataFrame inputs (skip_check_array=preserve_Xwherepreserve_X = nw.dependencies.is_pandas_dataframe(X)).X[:, features]with_safe_indexing(X, features, axis=1)across_rfe_single_fit,RFE._fit, andRFECV.fit.sklearn/feature_selection/tests/test_rfe.py(test_rfe_pandas_dataframe_preservationandtest_rfecv_pandas_dataframe_preservation) verifying feature name propagation and.predict()execution on DataFrames usingRandomForestClassifieras the estimator.Introduce yourself
I am a software developer and machine learning enthusiast, making my first contribution to scikit-learn. I love using scikit-learn and wanted to contribute by helping resolve open feature requests.
AI usage disclosure
I used AI assistance for:
sklearn.utils)Any other comments?
sklearn/feature_selection/tests/test_rfe.pypass cleanly.