0
$\begingroup$

in case feature encoding, if I'd like to encode my values based on my pre-determined dictionary, how do I do that?

For instance, say, I've values as [Red, Green, and Blue] and I want to encode them as [-1,0,1] -1 for red, 0 for Green, 1 for Blue... I'll apply it to my feature. I believe I can do it by mapping, apply method, not sure. But is there any better way to do that?

Column     expectedEncoding
Red             -1
Red             -1
Blue             1
Green            0
Red             -1
Blue             1

```
$\endgroup$
5
  • $\begingroup$ What technology are you using? What library? $\endgroup$
    – qmeeus
    Sep 10 '20 at 9:27
  • $\begingroup$ sklearn, pandas, numpy etc. $\endgroup$
    – quilliam
    Sep 10 '20 at 9:32
  • $\begingroup$ Then your best approach is just using df[col].map(mapping) where col is the name of the column to be encoded and mapping is a dictionary with the values $\endgroup$
    – qmeeus
    Sep 10 '20 at 9:36
  • $\begingroup$ Alright, I thought there might be another way for that. Well then, can I do it for different columns with different dictionaries at once? $\endgroup$
    – quilliam
    Sep 10 '20 at 10:29
  • $\begingroup$ I'll be answering this in a post $\endgroup$
    – qmeeus
    Sep 10 '20 at 11:17
1
$\begingroup$

Assuming you have a pandas DataFrame and one mapping per column, with all mappings stored in a 2-level dict where the keys of the first level correspond to the columns in the dataframe and the keys of the second level correspond to the categories:

{'fruit': {'banana': -1, 'apple': 1}, 'color': {'yellow': -1, 'red': 1}}

Then, you can do the following:

encoded_data = data.apply(lambda col: col.map(mappings[col.name]))

[EDIT] if have columns for which you don't have a mapping, you can do one of the following:

data.update(data[list(mappings)].apply(lambda col: col.map(mappings[col.name])))

or if you want it in a new dataframe (eg to keep the dataframe with the original values):

encoded_data = data.copy()
encoded_data.update(data[list(mappings)].apply(lambda col: col.map(mappings[col.name])))
$\endgroup$
4
  • $\begingroup$ There is key error if the column name is not in the dict $\endgroup$
    – quilliam
    Sep 10 '20 at 11:47
  • $\begingroup$ yes this is expected. You can filter the columns first if you want to avoid this $\endgroup$
    – qmeeus
    Sep 10 '20 at 12:07
  • $\begingroup$ Can you please also show me how to do that? $\endgroup$
    – quilliam
    Sep 10 '20 at 12:17
  • 1
    $\begingroup$ Check the updated answer $\endgroup$
    – qmeeus
    Sep 10 '20 at 12:24
0
$\begingroup$

You can use:

df.replace({'fruit': {'banana': -1, 'apple': 1}, 'color': {'yellow': -1, 'red': 1}},inplace=True)

given that 'fruit' and 'banana' are columns in your data-frame.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.