<jupyter_start><jupyter_text>Recuperando Lista de Variáveis Categóricas<jupyter_code>lista = (df_melbourne.dtypes == 'object')
object_cols = list(lista[lista].index)

print("Variáveis Categóricas:")
print(object_cols)<jupyter_output>Variáveis Categóricas:
['Type', 'Method', 'Regionname']<jupyter_text>(1) Removendo Atributos Categóricos<jupyter_code>drop_X_train = X_train.select_dtypes(exclude=['object'])
drop_X_valid = X_valid.select_dtypes(exclude=['object'])

print("MAE from Approach 1 (Drop categorical variables):")
print(score_dataset(drop_X_train, drop_X_valid, y_train, y_valid))<jupyter_output>MAE from Approach 1 (Drop categorical variables):
175751.0924308624<jupyter_text>(2) Label Encoding<jupyter_code>np.unique(X_train.Method)
from sklearn.preprocessing import LabelEncoder

# Make copy to avoid changing original data 
label_X_train = X_train.copy()
label_X_valid = X_valid.copy()

# Apply label encoder to each column with categorical data
label_encoder = LabelEncoder()
for col in object_cols:
    label_X_train[col] = label_encoder.fit_transform(X_train[col])
    label_X_valid[col] = label_encoder.transform(X_valid[col])

print("MAE from Approach 2 (Label Encoding):") 
print(score_dataset(label_X_train, label_X_valid, y_train, y_valid))
np.unique(label_X_train.Method)<jupyter_output><empty_output><jupyter_text>(3) One-Hot Encoder<jupyter_code>from sklearn.preprocessing import OneHotEncoder

# Make copy to avoid changing original data 
label_X_train = X_train.copy()
label_X_valid = X_valid.copy()

label_encoder = LabelEncoder()
for col in object_cols:
    _encoder = label_encoder.fit(pd.concat([X_valid[col], X_train[col]], axis=0))
    label_X_valid[col] = _encoder.transform(X_valid[col])
    label_X_train[col] = _encoder.transform(X_train[col])

# Apply one-hot encoder to each column with categorical data
OH_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
OH_cols_train = pd.DataFrame(OH_encoder.fit_transform(label_X_train[object_cols]))
OH_cols_valid = pd.DataFrame(OH_encoder.transform(label_X_valid[object_cols]))

# One-hot encoding removed index; put it back
OH_cols_train.index = X_train.index
OH_cols_valid.index = X_valid.index

# Remove categorical columns (will replace with one-hot encoding)
num_X_train = X_train.drop(object_cols, axis=1)
num_X_valid = X_valid.drop(object_cols, axis=1)

# Add one-hot encoded columns to numerical features
OH_X_train = pd.concat([num_X_train, OH_cols_train], axis=1)
OH_X_valid = pd.concat([num_X_valid, OH_cols_valid], axis=1)

print("MAE from Approach 3 (One-Hot Encoding):") 
print(score_dataset(OH_X_train, OH_X_valid, y_train, y_valid))
OH_X_train.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10966542750929369, 'fraction_numerical': 0.010408921933085501, 'mean_word_length': 8.215753424657533, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '215488', 'license': 'MIT', 'n_tokens_mistral': 1001, 'n_tokens_neox': 956, 'n_words': 184, 'path': '001 - Feature Engineering/.ipynb_checkpoints/Categorical_Encoding-checkpoint.ipynb', 'repo_name': 'howardroatti/Machine_Learning', 'size': '2690'}