3

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?

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
cocojambo
  • 63
  • 2
  • 5

1 Answers1

3

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]]
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33