Categorical encodings
Your colleague has converted the columns in the credit dataset to numeric values using LabelEncoder(). He left one out: credit_history, which records the credit history of the applicant. You want to create two versions of the dataset. One will use LabelEncoder() and another one-hot encoding, for comparison purposes. The feature matrix is available to you as credit. You have LabelEncoder() preloaded and pandas as pd.
Cet exercice fait partie du cours
Designing Machine Learning Workflows in Python
Instructions
- Encode
credit_historyusingLabelEncoder(). - Concatenate the result to the original frame.
- Create a new data frame by concatenating the 1-hot encoding dummies to the original frame.
- Confirm that 1-hot encoding produces more columns than label encoding.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Create numeric encoding for credit_history
credit_history_num = ____.____(
credit[____])
# Create a new feature matrix including the numeric encoding
X_num = pd.concat([X, pd.Series(____)], ____)
# Create new feature matrix with dummies for credit_history
X_hot = pd.concat(
[X, ____.____(credit[____])], ____)
# Compare the number of features of the resulting DataFrames
print(X_hot.shape[____] > X_num.shape[____])