0

I've been using LabelEncoder to transform my data into floats.

The problem is that I can an Date value that I also want to transform.

This is the code that I am using:

from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()
le.fit(df_train['Departure'])
df_train['DateOfDeparture'] = le.transform(df_train['DateOfDeparture'])

And this is the error that I get:

ValueError: y contains previously unseen labels: '2011-12-05'

Does anyone knows how to transform Dates correctly?

  • You should be able to do it in one line with `le.fit_transform(df_train['Departure'])` – G. Anderson Dec 17 '18 at 21:46
  • Both for train and for test? – Anna Noukou Dec 17 '18 at 21:48
  • `fit_transform` for train, then `transform` for test. Not sure if this will solve your issue in the test set, though – G. Anderson Dec 17 '18 at 21:54
  • Could you post some example data so we can replicate the problem and understand the question better? I think you might be using LabelEncoder for the wrong task. Typically you fit and transform (or fit_transform) LabelEncoder on the same variable in different slices of a dataset, but you seem to be trying to use it on two different variables. – ajrwhite Dec 17 '18 at 21:55
  • [Here](https://stackoverflow.com/questions/21057621/sklearn-labelencoder-with-never-seen-before-values) is a discussion about using pandas get_dummies instead because of this issue – G. Anderson Dec 17 '18 at 21:56
  • 1
    @G.Anderson is right that LabelEncoder can break on unseen values, but in your case you are using an entirely new column (DateOfDeparture) so none of the values will be the same. LabelEncoder isn't for "transforming data into floats", it's for creating a numeric coding of a categorical variable. You fit and transform on the same variable. – ajrwhite Dec 17 '18 at 22:00
  • LabelEncoder works only on a single column. For multiple columns you need to use multiple LabelEncoder objects. See this:https://stackoverflow.com/q/24458645/3374996 – Vivek Kumar Dec 18 '18 at 06:37
  • if you want to do the `fit_transform()` in a programme and to do the `transform()` in another programme please check this answer https://stackoverflow.com/questions/28656736/using-scikits-labelencoder-correctly-across-multiple-programs/55895639#55895639 – Shady Mohamed Sherif Apr 29 '19 at 00:18

0 Answers0