0
0
ML Pythonprogramming~10 mins

Handling categorical variables 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 convert the 'color' column to categorical type.

ML Python
df['color'] = df['color'].[1]()
Drag options to blanks, or click blank then click option'
Ato_numeric
Bastype('category')
Cfillna
Ddropna
Attempts:
3 left
2fill in blank
medium

Complete the code to create one-hot encoded columns for the 'color' categorical variable.

ML Python
encoded_df = pd.get_dummies(df['color'], [1]=False)
Drag options to blanks, or click blank then click option'
Adrop_first
Bprefix
Ccolumns
Ddummy_na
Attempts:
3 left
3fill in blank
hard

Fix the error in encoding the 'size' column using LabelEncoder.

ML Python
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df['size_encoded'] = le.[1](df['size'])
Drag options to blanks, or click blank then click option'
Afit
Btransform
Cfit_transform
Dencode
Attempts:
3 left
4fill in blank
hard

Fill both blanks to create a dictionary mapping categories to numbers using pandas.

ML Python
mapping = {cat: num for num, cat in enumerate(df['category'].[1]())}
encoded = df['category'].[2](mapping)
Drag options to blanks, or click blank then click option'
Aunique
Bmap
Cvalue_counts
Dreplace
Attempts:
3 left
5fill in blank
hard

Fill all three blanks to create a pipeline that imputes missing values, encodes categories, and fits a model.

ML Python
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
pipeline = Pipeline([
    ('imputer', SimpleImputer(strategy=[1])),
    ('encoder', OneHotEncoder(handle_unknown=[2])),
    ('model', [3]())
])
Drag options to blanks, or click blank then click option'
A'most_frequent'
B'ignore'
CLogisticRegression
D'mean'
Attempts:
3 left