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.
Bu egzersiz
Image Modeling with Keras
kursunun bir parçasıdırEgzersiz talimatları
- Initialize the
ohe_labelsvariable 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
1into the correct row/column combination in every iteration.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
# 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[____] = ____