Created
June 11, 2021 05:38
-
-
Save tommydangerous/ce8482b368350766ee0ae395a583bdd9 to your computer and use it in GitHub Desktop.
encode_values.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.preprocessing import OneHotEncoder | |
categorical_columns = ['Pclass', 'Sex', 'Embarked', 'cabin_letter'] | |
categorical_encoder = OneHotEncoder(handle_unknown='ignore') | |
categorical_encoder.fit(df[categorical_columns]) | |
# Add the new columns to the data | |
new_column_names = [] | |
for idx, cat_column_name in enumerate(categorical_columns): | |
values = categorical_encoder.categories_[idx] | |
new_column_names += [f'{cat_column_name}_{value}' for value in values] | |
df.loc[:, new_column_names] = \ | |
categorical_encoder.transform(df[categorical_columns]).toarray() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment