# How can I mathematically represent the one-hot encoding?

If we have 5 classes and 3 inputs, let's say [C1, C2, C3, C4, C5] and [X1, X2, X3] then,

• If X1 belongs to the C4 class then the hot encoder for this will be [0, 0, 0, 1, 0].

• If X1 belongs to the C2 class then the hot encoder for this will be [0, 1, 0, 0, 0].

How can I mathematically represent it?

I've also seen math-oriented people refer to a one-hot vector using an indicator function, but I am unable to understand this:

$$1_A(x) := \begin{cases} 1 & \text{if}\ x \in A, \\ 0 & \text{if}\ x \notin A. \end{cases}$$

Can anyone explain this above scenario to me?

$$A$$ is a set. If $$x$$ is an element of $$A$$, return $$1$$. Else return $$0$$. Thus $$A$$ is the set of cases that you are assigning a $$1$$ to in your encoding vector. Thus one-hot encoding is a vector form of this indicator function that applies componentwise.

• I know this, but how can this one will be assigned to the index in set A? just like If C4 is in X1 class then the index of one will be the 4 and rest will be zero, how this is being happening there? Commented Mar 14, 2022 at 6:24

The mathematical representation of one-hot encoding can indeed be done using indicator functions. An indicator function is a mathematical function that takes a value and returns 1 if the value satisfies a certain condition (belongs to a particular set), and 0 if it doesn't satisfy the condition.

C={C1​,C2​,C3​,C4​,C5​}, and you want to encode an input XX as a one-hot vector. You can define indicator functions for each category:

For category CiCi​, the indicator function 1Ci(X)1Ci​​(X) is defined as: {1if X=Ci0otherwise {10​if X=Ci​otherwise​

In this definition, 1Ci(X)1Ci​​(X) is 1 if XX belongs to category CiCi​, and 0 otherwise. Essentially, it's a function that acts as a "switch" to turn on (1) or off (0) for each category based on whether XX belongs to that category.

If X1X1​ belongs to the C4C4​ class, the one-hot encoding can be represented as: