Complete the code to import the library for encoding categorical variables.
from sklearn.preprocessing import [1]
The OneHotEncoder is used to convert categorical variables into a format that can be provided to ML algorithms.
Complete the code to create an encoder object for categorical data.
encoder = [1](sparse=False)
OneHotEncoder creates binary columns for each category. Setting sparse=False returns a dense array.
Fix the error in the code to encode the 'color' column of the DataFrame.
encoded = encoder.fit_transform(df[['[1]']])
The column name is 'color' exactly as in the DataFrame. Case and spelling must match.
Fill both blanks to create a dictionary mapping each category to its encoded column index.
mapping = {category: [1] for category, [2] in zip(encoder.categories_[0], range(len(encoder.categories_[0])))}We map each category to its index. The second variable in zip is the index, here named idx.
Fill all three blanks to create a DataFrame from the encoded array with proper column names.
encoded_df = pd.DataFrame(encoded, columns=[[1] + '_' + str([2]) for [3] in encoder.categories_[0]])
We create column names by combining each category with its index i. The loop variable is category.