Search code examples
pythonone-hot-encoding

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?


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]]