0
0
ML Pythonml~10 mins

One-hot encoding in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the one-hot encoder from scikit-learn.

ML Python
from sklearn.preprocessing import [1]
Drag options to blanks, or click blank then click option'
AMinMaxScaler
BLabelEncoder
CStandardScaler
DOneHotEncoder
Attempts:
3 left
💡 Hint
Common Mistakes
Using LabelEncoder instead of OneHotEncoder, which encodes labels as integers, not one-hot vectors.
2fill in blank
medium

Complete the code to create a one-hot encoder that outputs dense arrays.

ML Python
encoder = OneHotEncoder(sparse=[1])
Drag options to blanks, or click blank then click option'
AFalse
B0
CNone
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving sparse as True, which returns a sparse matrix instead of a dense array.
3fill in blank
hard

Fix the error in the code to fit the encoder to the data.

ML Python
encoder.fit([1])
Drag options to blanks, or click blank then click option'
Alabels
BX_train
Cdata
Dy_train
Attempts:
3 left
💡 Hint
Common Mistakes
Fitting the encoder on labels or target variables instead of features.
4fill in blank
hard

Fill both blanks to transform the data and convert the result to an array.

ML Python
encoded = encoder.[1]([2]).toarray()
Drag options to blanks, or click blank then click option'
Atransform
Bfit_transform
CX_test
DX_train
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform instead of fit_transform on training data.
Applying fit_transform on test data instead of training data.
5fill in blank
hard

Fill all three blanks to create a one-hot encoded DataFrame with proper column names.

ML Python
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])
Drag options to blanks, or click blank then click option'
Afit_transform
BX
Ccolumns
Dtransform
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform instead of fit_transform on the data.
Passing wrong argument to get_feature_names_out.
Not assigning column names when creating the DataFrame.