0
0
Data Analysis Pythondata~10 mins

Encoding categorical variables in Data Analysis 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 library for encoding categorical variables.

Data Analysis Python
from sklearn.preprocessing import [1]
Drag options to blanks, or click blank then click option'
AMinMaxScaler
BStandardScaler
COneHotEncoder
DPCA
Attempts:
3 left
💡 Hint
Common Mistakes
Importing scalers instead of encoders.
Confusing PCA with encoding.
2fill in blank
medium

Complete the code to create an encoder object for categorical data.

Data Analysis Python
encoder = [1](sparse=False)
Drag options to blanks, or click blank then click option'
AOneHotEncoder
BMinMaxScaler
CStandardScaler
DLabelEncoder
Attempts:
3 left
💡 Hint
Common Mistakes
Using LabelEncoder which returns single column labels.
Using scalers which are for numeric data.
3fill in blank
hard

Fix the error in the code to encode the 'color' column of the DataFrame.

Data Analysis Python
encoded = encoder.fit_transform(df[['[1]']])
Drag options to blanks, or click blank then click option'
AColor
Bcolor
Ccolors
Dcolour
Attempts:
3 left
💡 Hint
Common Mistakes
Using plural or misspelled column names.
Ignoring case sensitivity.
4fill in blank
hard

Fill both blanks to create a dictionary mapping each category to its encoded column index.

Data Analysis Python
mapping = {category: [1] for category, [2] in zip(encoder.categories_[0], range(len(encoder.categories_[0])))}
Drag options to blanks, or click blank then click option'
Aindex
Bidx
Ccategory
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping variable names.
Using the same name for both variables.
5fill in blank
hard

Fill all three blanks to create a DataFrame from the encoded array with proper column names.

Data Analysis Python
encoded_df = pd.DataFrame(encoded, columns=[[1] + '_' + str([2]) for [3] in encoder.categories_[0]])
Drag options to blanks, or click blank then click option'
Acategory
Bi
Ccategories
Didx
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names causing errors.
Mixing up loop variable and index.