Skip to content
Advertisement

How to create a one-hot-encoding for the intermediate class?

Let’s say I have 3 classes: 0, 1, 2
One-hot-encoding an array of labels can be done via pandas as follows:

enter image description here

What I’m interested in, is how to get an encoding that can handle an intermediate class, e.g. class in the middle between 2 classes.
For example:

  • for class 0.4, resulting encoding should be [0.4, 0.6, 0]
  • for class 1.8, resulting encoding should be [0, 0.2, 0.8]

Does anybody know such an encoder?
Thanks for your answer!

Advertisement

Answer

You can write a function for your strange encoding like the below:

JavaScript

Output:

JavaScript

We can write a function for generating a dataframe for encoding like below:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement