6

My data consists of 50 columns and most of them are strings. I have a single multi-class variable which I have to predict. I tried using LabelEncoder in scikit-learn to convert the features (not classes) into whole numbers and feed them as input to the RandomForest model I am using. I am using RandomForest for classification.

Now, when new test data comes (stream of new data), for each column, how will I know what the label for each string will be since using LabelEncoder now will give me a new label independent of the labels I generated before. Am, I doing this wrong? Is there anything else I should use for consistent encoding?

Huga
  • 541
  • 1
  • 7
  • 20
  • Seems related: http://stackoverflow.com/questions/21057621/sklearn-labelencoder-with-never-seen-before-values – lmjohns3 Jun 17 '15 at 06:27

2 Answers2

5

You could save the mapping: string -> label in training data with each column.

>>> from sklearn import preprocessing
>>> le = preprocessing.LabelEncoder()
>>> col_1 = ["paris", "paris", "tokyo", "amsterdam"]
>>> set_col_1 = list(set(col_1))
>>> le.fit(col_1)
>>> dict(zip(set_col_1, le.transform(set_col_1)))
{'amsterdam': 0, 'paris': 1, 'tokyo': 2}

When the testing data come, you could use those mapping to encode corresponding columns in testing data. You do not have to use encoder again in testing data.

DennisLi
  • 2,708
  • 3
  • 16
  • 35
Chung-Yen Hung
  • 321
  • 1
  • 6
3

The LabelEncoder class has two methods that handle this distinction: fit and transform. Typically you call fit first to map some data to a set of integers:

>>> le = LabelEncoder()
>>> le.fit(['a', 'e', 'b', 'z'])
>>> le.classes_
array(['a', 'b', 'e', 'z'], dtype='U1')

Once you've fit your encoder, you can transform any data to the label space, without changing the existing mapping:

>>> le.transform(['a', 'e', 'a', 'z', 'a', 'b'])
[0, 2, 0, 3, 0, 1]
>>> le.transform(['e', 'e', 'e'])
[2, 2, 2]

The use of this encoder basically assumes that you know beforehand what all the labels are in all of your data. If you have labels that might show up later (e.g., in an online learning scenario), you'll need to decide how to handle those outside the encoder.

lmjohns3
  • 6,586
  • 3
  • 30
  • 52