Using one-hot encoding to represent images
Neural networks expect the labels of classes in a dataset to be organized in a one-hot encoded manner: each row in the array contains zeros in all columns, except the column corresponding to a unique label, which is set to 1.
The fashion dataset contains three categories:
- Shirts
- Dresses
- Shoes
In this exercise, you will create a one-hot encoding of a small sample of these labels.
This is a part of the course
“Image Modeling with Keras”
Exercise instructions
- Initialize the
ohe_labels
variable to hold the one-hot encoded array. - Use
np.where()
to find the location of the category of the item in each iteration incategories
. - Assign a
1
into the correct row/column combination in every iteration.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# The number of image categories
n_categories = 3
# The unique values of categories in the data
categories = np.array(["shirt", "dress", "shoe"])
# Initialize ohe_labels as all zeros
ohe_labels = ____((len(labels), n_categories))
# Loop over the labels
for ii in range(len(labels)):
# Find the location of this label in the categories variable
jj = np.where(___)
# Set the corresponding zero to one
ohe_labels[____] = ____