9951 explained code solutions for 126 technologies


python-kerasHow can I use Python and Keras to create a one-hot encoding?


One-hot encoding is a process by which categorical variables are converted into a form that could be provided to ML algorithms to do a better job in prediction. Python and Keras provide a number of ways to perform one-hot encoding.

Below is an example of how to use Python and Keras to create a one-hot encoding.

from keras.utils import to_categorical

# define example
data = [1, 3, 5, 2]

# one hot encode
encoded = to_categorical(data)
print(encoded)

Output example

[[0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]
 [0. 0. 1. 0. 0.]]

The code above consists of the following parts:

  1. from keras.utils import to_categorical imports the to_categorical function from Keras.
  2. data = [1, 3, 5, 2] defines the data to be encoded.
  3. encoded = to_categorical(data) performs one-hot encoding on the data.
  4. print(encoded) prints the encoded data.

Helpful links

Edit this code on GitHub