passthrough all columns in sklearn pipeline

Viewed 649

I am trying to join the result of a PCA to the original features, to do this I tried a FeatureUnion of the PCA with a column transformer that just passthrough all columns

feature_selector = FeatureUnion(
    [
        ("original", make_column_transformer(('drop', []), reminder='passthrough'),
        ("pca", PCA())
    ])
my_pipeline = make_pipeline(preprocessor, feature_selector, model)

But this seems a bit counter intuitive.
Is there is any cleaner way of doing this? maybe a feature selector that select all columns instead of column transformer?

3 Answers

I think maybe the cleanest approach is to use a FunctionTransformer. Note in particular that the default value of the parameter func gives you an "identity transformer":

[...] If func is None, then func will be the identity function.

To Add, I found another way of doing this.
Simply using make_pipeline('passthrough') and this code work as expected

feature_selector = FeatureUnion(
    [
        ("original", make_pipeline('passthrough'),
        ("pca", PCA())
    ])
my_pipeline = make_pipeline(preprocessor, feature_selector, model)

Here's another approach, using just the ColumnTransformer. The main ugliness here (IMO) is selecting all the columns in each transformer; there are a number of ways to specify that, but so far the cleanest seems to be a blank/default make_column_selector.

ColumnTransformer([
    ('pass', "passthrough", make_column_selector()),
    ('pca', PCA(), make_column_selector()),
])
Related