4. You tried to apply one-hot encoding on a categorical column but got an error: ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). What is the most likely cause and fix?
medium
A. The column has missing values; fill or drop them before encoding.
B. The column is numeric; convert it to string first.
C. The one-hot encoder only works on integers; convert categories to integers first.
D. The dataset is too large; reduce its size.
Solution
Step 1: Analyze the error message
The error mentions NaN (missing) or infinite values, which cause encoding to fail.
Step 2: Identify fix for missing values
Fill missing values with a placeholder or drop rows before encoding to avoid errors.
Final Answer:
The column has missing values; fill or drop them before encoding. -> Option A
Quick Check:
Handle missing data before encoding [OK]
Hint: Check for missing values before encoding [OK]
Common Mistakes:
Ignoring missing values in categorical columns
Trying to encode numeric columns as categories
Assuming dataset size causes this error
5. You have a dataset with a categorical feature education_level having values: ['high school', 'bachelor', 'master', 'phd']. You want to use this feature in a regression model where order matters. Which encoding method should you choose and why?
hard
A. One-hot encoding, because it creates separate columns for each category.
B. Random encoding, to avoid bias in the model.
C. Label encoding, because it preserves the order of categories as numbers.
D. No encoding needed, use categories as strings directly.
Solution
Step 1: Understand the nature of the feature
Education levels have a natural order: high school < bachelor < master < phd.
Step 2: Choose encoding that preserves order
Label encoding assigns numbers reflecting order, suitable for regression models that consider magnitude.
Final Answer:
Label encoding, because it preserves the order of categories as numbers. -> Option C
Quick Check:
Ordered categories use label encoding [OK]
Hint: Use label encoding for ordered categories [OK]
Common Mistakes:
Using one-hot encoding for ordered categories
Ignoring order importance in encoding
Trying to use strings directly in models
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.