使用独热编码表示图像
神经网络期望数据集中的类别标签采用独热编码的形式:数组中的每一行在所有列上都是 0,只有与唯一标签对应的那一列被设为 1。
本时尚数据集包含 3 个类别:
- Shirts(衬衫)
- Dresses(连衣裙)
- Shoes(鞋)
在本练习中,您将为这些标签的小样本创建独热编码。
本练习是课程的一部分
使用 Keras 进行图像建模
练习说明
- 初始化变量
ohe_labels,用于存放独热编码后的数组。 - 使用
np.where()在categories中的每次迭代里找到该物品类别的位置。 - 在每次迭代中,将正确的行/列组合赋值为
1。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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[____] = ____