Skip to content

Instantly share code, notes, and snippets.

@Wei-1
Last active September 11, 2021 16:08
Show Gist options
  • Save Wei-1/2333c178262e9ad4e9b93a7a79157fa7 to your computer and use it in GitHub Desktop.
Save Wei-1/2333c178262e9ad4e9b93a7a79157fa7 to your computer and use it in GitHub Desktop.
How to use OrdinalEncoder
from sklearn.preprocessing import OrdinalEncoder
oe = OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=-1)
# if only categorical data has missing value
# will need to handle differently if there are missing numerical data
train_preprocessed = oe.fit_transform(train_df.fillna("NaN"))
test_preprocessed = oe.transform(test_df.fillna("NaN"))
train_preprocessed = pd.DataFrame(train_preprocessed, columns=train_df.columns)
test_preprocessed = pd.DataFrame(test_preprocessed, columns=train_df.columns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment