-
-
Notifications
You must be signed in to change notification settings - Fork 26k
FIX FunctionTransformer.get_feature_names_out
when output is set to dataframe
#31573
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?
FIX FunctionTransformer.get_feature_names_out
when output is set to dataframe
#31573
Conversation
get_feature_names_out
when fitted with a dataframeget_feature_names_out
when fitted with a dataframe
get_feature_names_out
when fitted with a dataframeget_feature_names_out
when output is set to dataframe
d48690e
to
158d7ed
Compare
get_feature_names_out
when output is set to dataframeFunctionTransformer.get_feature_names_out
when output is set to dataframe
if _is_polars_df(X) or _is_pandas_df(X): | ||
head = X.head(1) | ||
else: | ||
head = X[:1] |
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.
use _safe_indexing
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! Updated code
head = X[:1] | ||
|
||
head_out = self.func(head) | ||
if _is_polars_df(head_out) or _is_pandas_df(head_out): |
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.
we don't need to check for pandas or polars, do we? It's done in _get_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.
Correct, missed that. Updated code
self.func | ||
and not self.feature_names_out | ||
and hasattr(self, "_sklearn_output_config") | ||
and self._sklearn_output_config["transform"] in {"pandas", "polars"} |
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.
and self._sklearn_output_config["transform"] in {"pandas", "polars"} | |
and self._sklearn_output_config["transform"] != "default" |
def get_feature_names_out(self, input_features=None): | ||
"""Get output feature names for transformation. | ||
|
||
This method is only defined if `feature_names_out` is not None. | ||
This method is only defined if `feature_names_out` is not None | ||
or if `set_output` is called with "pandas" or "polars". |
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.
or if `set_output` is called with "pandas" or "polars". | |
or if `set_output` is called with a non-`"default"`. |
@@ -363,7 +379,12 @@ def get_feature_names_out(self, input_features=None): | |||
- If `feature_names_out` is a callable, then it is called with two | |||
arguments, `self` and `input_features`, and its return value is | |||
returned by this method. | |||
- If `feature_names_out` is None and the output container is set to | |||
`pandas` or `polars`, then dataframe columns from output are used. |
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 here
Reference Issues/PRs
Fixes #28780
What does this implement/fix? Explain your changes.
When
FunctionTransformer
:feature_names_out
set to Noneset_output
called with "pandas" or "polars"Then
get_feature_names_out()
should return the output dataframe's columns. However, it raises an AttributeError instead.A specific example, copied from the issue's discussions:
Any other comments?
To get the output column names, I saw three options:
func
on the fitted data(1) means calling the function twice during a fit_transform, whereas (2) means making transform a stateful operation. (3) means that the specific example above, when called without an argument, would still fail.
I went with (1): during
fit
, callfunc
with a smaller slice of the input in order to get output dataframe column names. If that's not desirable let me know and I can change the approach.The fix works both for pandas and polars dataframes.