Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create One hot labels, one Hot encoding based on multiple condition

For example i have the following: [1,2,3,5] and I want to hot encode it. It usually looks like this:

[1,0,0,0,0]
[0,1,0,0,0]
[0,0,1,0,0]
[0,0,0,0,1]

But instead of that, I want to have a conditional one hot encoding and only two classes. All values below 3 get the value 1 and all values above or equal 3 get the value 0, like this:

[1,0]
[1,0]
[0,1]
[0,1]

I know how to do the first one, but I’m struggling on the second one. Can someone please help me?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use a list comprehension:

data = [1,2,3,5]
CUTOFF = 3
[[1, 0] if val < CUTOFF else [0, 1] for val in data]

This outputs:

[[1, 0], [1, 0], [0, 1], [0, 1]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading