ColumnTransformer 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.
1234567891011121314151617181920212223242526import 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)
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.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Can you explain how the ColumnTransformer handles missing values?
How do I integrate the ColumnTransformer with a machine learning model in a pipeline?
Can you show how to inverse transform the processed data back to its original form?
Genial!
Completion tasa mejorada a 5.26
ColumnTransformer for Mixed Data
Desliza para mostrar el menú
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.
1234567891011121314151617181920212223242526import 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)
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.
¡Gracias por tus comentarios!