Databricks Machine Learning Associate Question 529
Single answerYou are working on a machine learning project in Databricks where the dataset contains a categorical feature 'Color' with possible values: 'Red', 'Blue', and 'Green'. You need to prepare this feature for use in a machine learning model that requires numerical input. How should you apply one-hot encoding to this feature in Databricks?
- A
Use the
StringIndexerclass to index the 'Color' column and directly use the indexed column as input to the model. - B
Use the
OneHotEncoderclass to transform the 'Color' column into multiple binary columns, one for each unique value. - C
Manually create three new columns, each representing one possible value of 'Color', and set their values to 0 or 1 based on the original column's value.
- D
Drop the 'Color' column entirely as it cannot be directly used for numerical input.
Show answer and explanation
Correct answer: B
Explanation
One-hot encoding is a method to convert categorical features into a format that can be provided to machine learning algorithms. In Databricks, the OneHotEncoder class is used to achieve this by creating binary columns for each unique value in the categorical column. This ensures the model can interpret the categorical data correctly without introducing ordinal relationships.
- A. Incorrect.
StringIndexer is used to convert categorical values into numerical indices, but it does not produce the binary columns required for one-hot encoding.
- B. Correct.
OneHotEncoder is the correct approach for one-hot encoding as it automatically creates binary columns for each unique value of the categorical feature.
- C. Incorrect.
Manually creating binary columns is not efficient and error-prone compared to using the OneHotEncoder, which is specifically designed for this purpose.
- D. Incorrect.
Dropping the 'Color' column would result in losing important information and is not a valid approach unless the feature is irrelevant.