Ask Your Question

How can pipelines be used in combination with various make_column_transformers for different columns?

asked 2023-02-08 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-10-19 22:00:00 +0000

lakamha gravatar image

Pipelines can be used in combination with various makecolumntransformers by specifying the transformations for each column or group of columns within the makecolumntransformers function, and then passing the resulting transformer to the pipeline.

For example, suppose we have a dataset with columns "age", "income", "gender", and "education". We may want to apply different transformations to each of these columns before fitting a model.

We can define separate transformers for each column using makecolumntransformers, like so:

from sklearn.compose import make_column_transformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder

# Define transformers for each column
ct = make_column_transformer(
    (StandardScaler(), ['age', 'income']),
    (OneHotEncoder(), ['gender', 'education']),

Here, we use StandardScaler to standardize the numeric columns "age" and "income", and OneHotEncoder to one-hot encode the categorical columns "gender" and "education".

Next, we can pass the entire transformer (ct) to a pipeline, along with any additional steps, such as applying a model:

from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression

# Define the pipeline
clf = make_pipeline(

Here, we create a pipeline that applies the column transformations defined in ct, followed by LogisticRegression.

Using pipelines and makecolumntransformers in this way allows us to easily apply distinct transformations to each column, while still keeping the code organized and easy to read. Additionally, we can easily add or modify the transformations applied to each column without having to refactor the entire pipeline.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2023-02-08 11:00:00 +0000

Seen: 6 times

Last updated: Oct 19 '21