Skip to content

ColumnTransformer output unexpected prefixed feature names from FunctionTransformer() step #28260

Closed

Description

@J-ZW-Wang

Describe the bug

The following code demonstrates that when FunctionTransformer is present as a step in ColumnTransformer, the feature names output are all prefixed with the name from the last step 'C__'. For example, column 'x1' is output as 'C__x1' for 3 times.
When 'FunctionTransferformer' is not present as a step, the feature names are corrected prefixed by the name in each steps. For example, column 'x1' is output as 'A__x1' and 'C__x1' respectively, as expected.

Steps/Code to Reproduce

import pandas as pd
from sklearn.preprocessing import FunctionTransformer, StandardScaler
from sklearn.compose import ColumnTransformer
from sklearn import set_config
set_config(transform_output='pandas') 

df = pd.DataFrame({
        'x1' : [1, 2, 3],
        'x2' : [10, 20, 30],
        'x3' : [100, 200, 300]})

my_coltransformer = ColumnTransformer([
    ('A', 'passthrough', ['x1', 'x2', 'x3']),
    ('B', FunctionTransformer(lambda x: x**2), ['x1', 'x2', 'x3']),
    ('C', StandardScaler(), ['x1', 'x2', 'x3'])])
transformed_df = my_coltransformer.fit_transform(df)
print(transformed_df)

my_coltransformer = ColumnTransformer([
    ('A', 'passthrough', ['x1', 'x2', 'x3']),
    #('B', FunctionTransformer(lambda x: x**2), ['x1', 'x2', 'x3']),
    ('C', StandardScaler(), ['x1', 'x2', 'x3'])])
transformed_df = my_coltransformer.fit_transform(df)
print(transformed_df)

Expected Results

   A__x1  A__x2  A__x3  B__x1  B__x2  B__x3     C__x1     C__x2     C__x3
0      1     10    100      1    100  10000 -1.224745 -1.224745 -1.224745
1      2     20    200      4    400  40000  0.000000  0.000000  0.000000
2      3     30    300      9    900  90000  1.224745  1.224745  1.224745

Actual Results

   C__x1  C__x2  C__x3  C__x1  C__x2  C__x3     C__x1     C__x2     C__x3
0      1     10    100      1    100  10000 -1.224745 -1.224745 -1.224745
1      2     20    200      4    400  40000  0.000000  0.000000  0.000000
2      3     30    300      9    900  90000  1.224745  1.224745  1.224745

Versions

1.4.0
I tested this in 1.3.0 and did not observe the unexpected results.

Activity

  1. added
    Needs Triage
    on Jan 25, 2024
  2. changed the title [-]ColumnTransFormer output unexpected prefixed feature names from FunctionTransformer() step[/-] [+]ColumnTransformer output unexpected prefixed feature names from FunctionTransformer() step[/+] on Jan 25, 2024
  3. removed
    Needs Triage
    on Jan 25, 2024
  4. glemaitre commented on Jan 25, 2024

    @glemaitre
    Member

    I can reproduce the issue. I'll investigate the reason.

  5. glemaitre commented on Jan 25, 2024

    @glemaitre
    Member

    As a temporary workaround, setting feature_names_out="one-to-one" provide the expected results.

  6. glemaitre commented on Jan 25, 2024

    @glemaitre
    Member

    We indeed refactor and use a dictionary to rename the columns: https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/utils/_set_output.py#L125

    But we overlooked that one could reuse the same column and then x1 get remapped to the latest transformer. We should therefore pass a list instead.

  7. glemaitre commented on Jan 25, 2024

    @glemaitre
    Member

    I'll submit a patch.

    /take

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions