1
$\begingroup$

I have a dataset and one of the attributes of the dataset is Race. People have multiple races on the dataset. The values for the attribute Race are following

Race
A
A,C
B
A,B,C

I cannot understand how I encode those values. If there is no multiple items for one value then I can encode it using label encoder.

I try to say that if Race is like

Race
A
B
C

I can label encode it like

Race
1
2
3

Any suggestion would be helpful.

$\endgroup$
3
  • $\begingroup$ This is unclear. Do you say that some people identify with multiple races? $\endgroup$ Nov 26, 2022 at 15:01
  • $\begingroup$ Yes. The dataset described some people with multiple races. $\endgroup$
    – Encipher
    Nov 26, 2022 at 21:48
  • $\begingroup$ OK, then please add that as an edit to the post, together with a description of your research goals $\endgroup$ Nov 26, 2022 at 22:19

1 Answer 1

3
$\begingroup$

You can encode it as multi-hot vectors, ie set a 1 for every category that appears in the comma separated label. This results in one row having potentially multiple ones ( instead of one hot encoding).

See What exactly is multi-hot encoding and how is it different from one-hot? for details and examples.

Update after comment:

In your concrete example a Race value of "A,C" would be multi-hot encoded as a row vector [1,0,1] , "A" as [1,0,0], etc. The total vocabulary size determines the number of elements of that vector.

See this post for an example of how to do this with pandas/sklearn.

$\endgroup$
5
  • $\begingroup$ Can you please give me an example? You can use the example of my question. $\endgroup$
    – Encipher
    Nov 28, 2022 at 4:26
  • 1
    $\begingroup$ @Encipher updated reply with an example $\endgroup$ Nov 28, 2022 at 4:31
  • $\begingroup$ I am assuming your gave this example depending upon Pandas. Can you please share the official page of the documentation? Is there any way to use label encoding instead of one hot encoding? $\endgroup$
    – Encipher
    Nov 28, 2022 at 4:36
  • $\begingroup$ I m not aware of a pandas function off-the-shelf, but I reckon a combination of 'split(,)' or 'explode()' and pivot will accomplish this. Re mean encoding: If you want to mean encode it then just treat the list as it's own category without splitting it up (but make sure to sort values to avoid doue counting sets). This is IMHO though out of scope of the original question. $\endgroup$ Nov 28, 2022 at 4:41
  • $\begingroup$ @Encipher Actually just found this post which explains how to do this in pandas / sklearn stackoverflow.com/questions/67108935/… . added this to my response $\endgroup$ Nov 29, 2022 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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