開始使用免費開始

用 one-hot encoding 表示影像

神經網路通常期望資料集中的類別標籤以 one-hot encoding 的方式表示:陣列中的每一列在所有欄位都是 0,只有對應到該唯一標籤的那一欄設為 1。

這個服飾資料集包含 3 個類別:

  1. 襯衫
  2. 洋裝
  3. 鞋子

在這個練習中,你會為一小部分標籤建立 one-hot encoding。

本練習屬於課程

使用 Keras 進行影像建模

檢視課程

練習說明

  • 初始化變數 ohe_labels,用來存放 one-hot 編碼後的陣列。
  • 使用 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[____] = ____
編輯並執行程式碼