
Answer-first summary for fast verification
Answer: from sklearn.preprocessing import OrdinalEncoder encoder = OrdinalEncoder(handle_unknown='ignore') encoder.fit(df[['Education Level']]) df['Education Level'] = encoder.transform(df[['Education Level']])
The correct code snippet to perform ordinal encoding on the 'Education Level' feature while handling missing values is 'from sklearn.preprocessing import OrdinalEncoder encoder = OrdinalEncoder(handle_unknown='ignore') encoder.fit(df[['Education Level']]) df['Education Level'] = encoder.transform(df[['Education Level']])'. The 'handle_unknown='ignore'' parameter ensures that missing values are ignored during the encoding process, allowing the model to handle them separately.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
Consider a scenario where you have a dataset with a categorical feature 'Education Level' having categories 'High School', 'Bachelor', 'Master', 'PhD', and missing values. Write a code snippet to perform ordinal encoding on this feature using Python and the scikit-learn library. Explain how this process handles missing values.
A
from sklearn.preprocessing import OrdinalEncoder encoder = OrdinalEncoder(handle_unknown='ignore') encoder.fit(df[['Education Level']]) df['Education Level'] = encoder.transform(df[['Education Level']])
B
from sklearn.preprocessing import OrdinalEncoder encoder = OrdinalEncoder() encoder.fit(df[['Education Level']]) df['Education Level'] = encoder.transform(df[['Education Level']])
C
from sklearn.preprocessing import OrdinalEncoder encoder = OrdinalEncoder(handle_unknown='error') encoder.fit(df[['Education Level']]) df['Education Level'] = encoder.transform(df[['Education Level']])
D
from sklearn.preprocessing import OrdinalEncoder encoder = OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=-1) encoder.fit(df[['Education Level']]) df['Education Level'] = encoder.transform(df[['Education Level']])
No comments yet.