Complete the code to import the one-hot encoder from scikit-learn.
from sklearn.preprocessing import [1]
The OneHotEncoder is the correct class to perform one-hot encoding in scikit-learn.
Complete the code to create a one-hot encoder that outputs dense arrays.
encoder = OneHotEncoder(sparse=[1])Setting sparse=False makes the encoder output dense numpy arrays instead of sparse matrices.
Fix the error in the code to fit the encoder to the data.
encoder.fit([1])The encoder should be fit on the feature data, usually named X_train, not labels or target variables.
Fill both blanks to transform the data and convert the result to an array.
encoded = encoder.[1]([2]).toarray()
Using fit_transform on X_train fits the encoder and transforms the training data in one step. The toarray() converts the sparse matrix to a dense array.
Fill all three blanks to create a one-hot encoded DataFrame with proper column names.
import pandas as pd encoded_array = encoder.[1](X).toarray() columns = encoder.get_feature_names_out([2]) encoded_df = pd.DataFrame(encoded_array, columns=[3])
First, use fit_transform on the data X. Then get the feature names with get_feature_names_out passing the original column names columns. Finally, create a DataFrame with the encoded array and column names.