Skip to content

Instantly share code, notes, and snippets.

@cjauvin
Created November 17, 2015 15:18
Show Gist options
  • Select an option

  • Save cjauvin/595e343720412a2cd24b to your computer and use it in GitHub Desktop.

Select an option

Save cjauvin/595e343720412a2cd24b to your computer and use it in GitHub Desktop.
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