Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn ColumnTransformer for Mixed Data | Pipelines and Composition Patterns
Mastering scikit-learn API and Workflows

bookColumnTransformer for Mixed Data

When working with real-world datasets, you often encounter tables that contain both numerical and categorical features. For instance, a customer dataset may include columns like age (numerical) and city (categorical). Since different types of data require different preprocessing stepsβ€”such as scaling for numerical features and encoding for categorical onesβ€”using a single transformer is not sufficient. This is where the ColumnTransformer becomes essential. It allows you to specify exactly which transformations should be applied to each subset of columns, enabling you to prepare heterogeneous data efficiently and consistently for modeling.

1234567891011121314151617181920212223242526
import pandas as pd from sklearn.compose import ColumnTransformer from sklearn.preprocessing import StandardScaler, OneHotEncoder # Sample DataFrame with mixed data types data = pd.DataFrame({ "age": [25, 32, 47, 51], "income": [50000, 64000, 120000, 98000], "city": ["New York", "San Francisco", "Chicago", "New York"] }) # Define columns by data type numeric_features = ["age", "income"] categorical_features = ["city"] # Create a ColumnTransformer preprocessor = ColumnTransformer( transformers=[ ("num", StandardScaler(), numeric_features), ("cat", OneHotEncoder(), categorical_features) ] ) # Fit and transform the data transformed = preprocessor.fit_transform(data) print(transformed)
copy

The ColumnTransformer is designed to work seamlessly with the Pipeline object you learned about earlier. By placing your preprocessor as the first step in a pipeline, you can chain together preprocessing and modeling into a single, reusable workflow. This approach keeps your code organized and ensures that each transformation is applied consistently during both training and prediction. Integrating a ColumnTransformer within a pipeline makes it easy to handle mixed data types and maintain a clean, production-ready workflow.

question mark

What is the main purpose of using a ColumnTransformer in scikit-learn when working with real-world datasets that contain both numerical and categorical features?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookColumnTransformer for Mixed Data

Swipe to show menu

When working with real-world datasets, you often encounter tables that contain both numerical and categorical features. For instance, a customer dataset may include columns like age (numerical) and city (categorical). Since different types of data require different preprocessing stepsβ€”such as scaling for numerical features and encoding for categorical onesβ€”using a single transformer is not sufficient. This is where the ColumnTransformer becomes essential. It allows you to specify exactly which transformations should be applied to each subset of columns, enabling you to prepare heterogeneous data efficiently and consistently for modeling.

1234567891011121314151617181920212223242526
import pandas as pd from sklearn.compose import ColumnTransformer from sklearn.preprocessing import StandardScaler, OneHotEncoder # Sample DataFrame with mixed data types data = pd.DataFrame({ "age": [25, 32, 47, 51], "income": [50000, 64000, 120000, 98000], "city": ["New York", "San Francisco", "Chicago", "New York"] }) # Define columns by data type numeric_features = ["age", "income"] categorical_features = ["city"] # Create a ColumnTransformer preprocessor = ColumnTransformer( transformers=[ ("num", StandardScaler(), numeric_features), ("cat", OneHotEncoder(), categorical_features) ] ) # Fit and transform the data transformed = preprocessor.fit_transform(data) print(transformed)
copy

The ColumnTransformer is designed to work seamlessly with the Pipeline object you learned about earlier. By placing your preprocessor as the first step in a pipeline, you can chain together preprocessing and modeling into a single, reusable workflow. This approach keeps your code organized and ensures that each transformation is applied consistently during both training and prediction. Integrating a ColumnTransformer within a pipeline makes it easy to handle mixed data types and maintain a clean, production-ready workflow.

question mark

What is the main purpose of using a ColumnTransformer in scikit-learn when working with real-world datasets that contain both numerical and categorical features?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt