Last active
September 11, 2021 16:08
-
-
Save Wei-1/2333c178262e9ad4e9b93a7a79157fa7 to your computer and use it in GitHub Desktop.
How to use OrdinalEncoder
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 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