Created
November 17, 2015 15:18
-
-
Save cjauvin/595e343720412a2cd24b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 LabelEncoder | |
| import pandas as pd | |
| import bisect | |
| le = LabelEncoder() | |
| train_c = le.fit_transform(['a', 'b', 'c', 'a']) | |
| test_c = pd.Series(['a', 'b', 'c', 'd']).map(lambda s: '<unk>' if s not in le.classes_ else s) | |
| print(test_c.tolist()) # ['a', 'b', 'c', '<unk>'] | |
| # very clunky! | |
| le_classes = le.classes_.tolist() | |
| bisect.insort_left(le_classes, '<unk>') | |
| le.classes_ = le_classes | |
| print(le.transform(test_c)) # [1, 2, 3, 0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment