0
0
ML Pythonml~20 mins

One-hot encoding in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
One-hot Encoding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this one-hot encoding code?
Consider this Python code using pandas to one-hot encode a categorical column. What is the printed output?
ML Python
import pandas as pd

data = {'color': ['red', 'blue', 'green', 'blue']}
df = pd.DataFrame(data)
dummies = pd.get_dummies(df['color'])
print(dummies)
A
   red  blue  green
0    1     0      0
1    0     1      0
2    0     0      1
3    0     1      0
B
   blue  green  red
0     0      0    1
1     1      0    0
2     0      1    0
3     1      0    0
C
   green  red  blue
0      0    1     0
1      0    0     1
2      1    0     0
3      0    0     1
D
   red  green  blue
0    1      0     0
1    0      0     1
2    0      1     0
3    0      0     1
Attempts:
2 left
💡 Hint
Look at the order of columns created by pd.get_dummies and how the rows correspond to the original data.
🧠 Conceptual
intermediate
1:30remaining
Why use one-hot encoding for categorical data?
Which of the following is the main reason to use one-hot encoding on categorical features before training a machine learning model?
ATo convert categories into numbers so the model can process them without assuming order.
BTo reduce the number of features and simplify the model.
CTo normalize the data between 0 and 1 for better training.
DTo combine multiple categories into a single numeric value.
Attempts:
2 left
💡 Hint
Think about how models interpret numeric inputs and what happens if categories are just assigned numbers.
Hyperparameter
advanced
2:00remaining
Choosing the right approach for high-cardinality categorical features
You have a categorical feature with 10,000 unique values. Which one-hot encoding approach is best to avoid excessive memory use?
AUse standard one-hot encoding creating 10,000 binary columns.
BUse label encoding assigning integers to categories.
CUse one-hot encoding but drop one category to reduce columns by one.
DUse target encoding to replace categories with average target values.
Attempts:
2 left
💡 Hint
Think about memory and model performance when many categories exist.
Metrics
advanced
1:30remaining
Effect of one-hot encoding on model accuracy
You train two models on the same dataset: Model A uses label encoding for a categorical feature, Model B uses one-hot encoding. Model B shows better accuracy. Why?
ALabel encoding always causes models to overfit, reducing accuracy.
BOne-hot encoding reduces the number of features, making training faster and more accurate.
COne-hot encoding prevents the model from assuming an order in categories, improving accuracy.
DLabel encoding converts categories to strings, which models cannot process.
Attempts:
2 left
💡 Hint
Consider how models interpret numeric values from label encoding.
🔧 Debug
expert
2:30remaining
Why does this one-hot encoding code raise an error?
What error does this code raise and why? import numpy as np categories = ['cat', 'dog', 'bird'] values = ['cat', 'dog', 'fish'] one_hot = np.zeros((len(values), len(categories))) for i, val in enumerate(values): idx = categories.index(val) one_hot[i, idx] = 1 print(one_hot)
AValueError: 'fish' is not in list because 'fish' is not in categories.
BIndexError: index out of range because 'fish' index is too large.
CTypeError: unsupported operand type(s) because of wrong data types.
DNo error, prints a one-hot encoded numpy array.
Attempts:
2 left
💡 Hint
Check if all values exist in the categories list before indexing.