-1

I am working on neural networks in python. I have a dataset which includes numerical and categorical variables together. My goal is change all categorical variables into some numerical values. For instance:

4   5   sep fri 94.3    85.1    692.3   15.9    17.7    37  3.6 0   0
5   4   mar fri 91.7    33.3    77.5    9   15.6    25  6.3 0   0
5   4   aug tue 88.8    147.3   614.5   9   17.3    43  4.5 0   0

As you can see month names and weekday names are categorical. Which I wanted is:

4   5   9   5   94.3    85.1    692.3   15.9    17.7    37  3.6 0   0
5   4   3   5   91.7    33.3    77.5    9   15.6    25  6.3 0   0
5   4   8   2   88.8    147.3   614.5   9   17.3    43  4.5 0   0

To make thing done, I suppose I have to use map() function.

month_mapping = {'jan': 1, 'feb': 2, 'mar': 3, 'apr': 4, 'may': 5,
                     'jun': 6, 'jul': 7, 'aug': 8, 'sep': 9, 'oct': 10, 'nov': 11, 'dec': 12}
day_mapping = {'mon': 1, 'tue': 2, 'wed': 3,
                   'thu': 4, 'fri': 5, 'sat': 6, 'sun': 7}

#month column of the array
print(data[:, 2])
test = map(month_mapping, data[:, 2])
print(test)

But in the map() function, it crashes. How can I do that?

martineau
  • 119,623
  • 25
  • 170
  • 301
Berkin
  • 1,565
  • 5
  • 22
  • 48
  • Ah I solved. https://stackoverflow.com/questions/16992713/translate-every-element-in-numpy-array-according-to-key – Berkin May 11 '18 at 16:39

1 Answers1

1

If you have to use the map function, you can do it like this:

months = ['jan', 'feb', 'marc']  #all months
mapped_month = list(map(lambda x: months.index(x), data[:, 2]))

Similarly you would do the same thing with the days.

ninesalt
  • 4,054
  • 5
  • 35
  • 75