Repository navigation
MNT Simplify hstack logic in ColumnTransformer - #32205
Conversation
Co-authored-by: Jérôme Dockès <[email protected]>
… into fix-feature-union-polars-output
| if callable(columns): | ||
| try: | ||
| return columns() | ||
| except Exception: | ||
| return None | ||
| except AttributeError as e: | ||
| if "does not provide get_feature_names_out" in str(e): | ||
| return None | ||
| else: | ||
| raise | ||
| return columns |
There was a problem hiding this comment.
The coverage failure is due to the except part that is not now covered. It's still necessary though because it's useful for FreatureUnion but not tested. Tests for FeatureUnion are added in #32106
adrinjalali
left a comment
There was a problem hiding this comment.
Can you please explain the changes in the tests? It feels odd to simplify the code, and at the same time having to change the tests so much 😁
| if hasattr(trans, "get_feature_names_out"): | ||
| column_indices = self._transformer_to_input_indices[name] | ||
| names = input_features[column_indices] | ||
| feature_names_out = trans.get_feature_names_out(names) | ||
| elif hasattr(self, "_transformers_feature_names_out"): | ||
| # Fallback to feature names returned by transformers that output | ||
| # dataframes but don't implement get_feature_names_out. | ||
| feature_names_out = self._transformers_feature_names_out[i] | ||
| else: | ||
| raise AttributeError( | ||
| f"Transformer {name} (type {type(trans).__name__}) does " | ||
| "not provide get_feature_names_out." | ||
| ) | ||
|
|
There was a problem hiding this comment.
Looks like it would have been cleaner to keep it in the helper function?
| skip_empty_columns=True, | ||
| # Store feature names out of transformers in case they don't implement | ||
| # get_feature_names_out | ||
| self._transformers_feature_names_out = [X.columns for X in Xs] |
There was a problem hiding this comment.
use _get_feature_names from validation instead?
| list(zip(transformer_names, feature_names_outs)) | ||
| ) | ||
| else: | ||
| # check for duplicated columns and raise if any |
There was a problem hiding this comment.
what's happening now with duplicate names then?
There was a problem hiding this comment.
It's detected by the set_output wrapper which raises an error. It was the expected behavior from the start but bypassed due to a bug. The previous bug fix in #28262 was overly complicated and introduced a custom check for this instead of finding a way to not bypass the set_output wrapper own check.
| transformer.set_params(verbose_feature_names_out=False) | ||
| with pytest.raises(ValueError, match=r"Output feature names:.*are not unique"): |
There was a problem hiding this comment.
The test that I removed was almost the same as this one except that it checked the verbose_feature_names_out=False part. That's why I merged both tests into a single one.
There was a problem hiding this comment.
Note that the error message is not the same because it's now handled by the set_output machinery as it was expected from the start.
| # DfOutTransformer that does not define get_feature_names_out | ||
| class DfOutTransformer(BaseEstimator): | ||
| def __init__(self, offset=1.0): | ||
| self.offset = offset | ||
|
|
||
| def fit(self, X, y=None): | ||
| return self | ||
|
|
||
| def transform(self, X, y=None): | ||
| return X - self.offset | ||
|
|
||
| def set_output(self, transform=None): | ||
| # This transformer will always output a DataFrame regardless of the | ||
| # configuration. | ||
| return self |
There was a problem hiding this comment.
I added that to have a more robust test for 3rd party estimators
Indeed :) I updated it to have a much smaller diff. The only 2 changes are now
|
|
Merging this maintenance PR with 1 approval. |
noticed while working on #32106. I extracted here the part that is only about the ColumnTransformer.
In #28262, a lot of complexity was added to the
_hstackmethod to raise an error when the concatenated dataframe would have duplicate column names. However this logic is already implemented inget_feature_names_out.We can simplify it a lot by storing the feature names and let
get_feature_names_outhandle the renaming and errors as it was designed to. At this stage, to avoid duplicate feature names, we can just use temporary unique names since they will be renamed anyway at the end by the set_output wrapper.I also merged the 2 existing tests that test this part since their only diff was
verbose_feature_names_out=True|False, and extended them to check a wider range of transformers, in particular with transformers that don't defineget_feature_names_out.