0
0
ML Pythonml~10 mins

ColumnTransformer for mixed types in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the correct class for transforming columns.

ML Python
from sklearn.compose import [1]
Drag options to blanks, or click blank then click option'
AStandardScaler
BOneHotEncoder
CColumnTransformer
DSimpleImputer
Attempts:
3 left
💡 Hint
Common Mistakes
Importing a transformer like StandardScaler instead of ColumnTransformer.
Confusing OneHotEncoder with ColumnTransformer.
2fill in blank
medium

Complete the code to create a ColumnTransformer that applies OneHotEncoder to categorical columns.

ML Python
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder

ct = ColumnTransformer(transformers=[('cat', [1](), ['color', 'type'])], remainder='passthrough')
Drag options to blanks, or click blank then click option'
AOneHotEncoder
BStandardScaler
CSimpleImputer
DPCA
Attempts:
3 left
💡 Hint
Common Mistakes
Using StandardScaler for categorical data.
Forgetting to import OneHotEncoder.
3fill in blank
hard

Fix the error in the code to apply StandardScaler to numeric columns.

ML Python
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler

ct = ColumnTransformer(transformers=[('num', [1](), ['age', 'income'])], remainder='passthrough')
Drag options to blanks, or click blank then click option'
AOneHotEncoder
BStandardScaler
CSimpleImputer
DLabelEncoder
Attempts:
3 left
💡 Hint
Common Mistakes
Using OneHotEncoder for numeric columns.
Using LabelEncoder which is for labels, not features.
4fill in blank
hard

Fill both blanks to create a ColumnTransformer that imputes missing values in numeric columns and encodes categorical columns.

ML Python
from sklearn.compose import ColumnTransformer
from sklearn.impute import [1]
from sklearn.preprocessing import [2]

ct = ColumnTransformer(transformers=[('num', [1](), ['age', 'income']), ('cat', [2](), ['gender', 'city'])], remainder='passthrough')
Drag options to blanks, or click blank then click option'
ASimpleImputer
BOneHotEncoder
CStandardScaler
DLabelEncoder
Attempts:
3 left
💡 Hint
Common Mistakes
Using StandardScaler instead of SimpleImputer for missing values.
Using LabelEncoder for categorical features in ColumnTransformer.
5fill in blank
hard

Fill all three blanks to create a pipeline that preprocesses numeric and categorical data, then fits a logistic regression model.

ML Python
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import [1]
from sklearn.impute import [2]
from sklearn.preprocessing import [3], StandardScaler

numeric_transformer = Pipeline(steps=[('imputer', [2]()), ('scaler', StandardScaler())])
categorical_transformer = Pipeline(steps=[('encoder', [3](handle_unknown='ignore'))])

ct = ColumnTransformer(transformers=[('num', numeric_transformer, ['age', 'income']), ('cat', categorical_transformer, ['gender', 'city'])])

model = Pipeline(steps=[('preprocessor', ct), ('classifier', [1]())])
Drag options to blanks, or click blank then click option'
ALogisticRegression
BSimpleImputer
COneHotEncoder
DRandomForestClassifier
Attempts:
3 left
💡 Hint
Common Mistakes
Using RandomForestClassifier instead of LogisticRegression as the model here.
Forgetting to use SimpleImputer in the numeric pipeline.
Not setting handle_unknown='ignore' in OneHotEncoder.