開始使用免費開始

探索 LED 數字資料集

在接下來的練習中,你會使用 NMF 將灰階影像分解成常見的局部圖樣。首先,先探索影像資料集,了解它如何以陣列編碼。你會拿到 100 張影像,存在 2D 陣列 samples 中,其中每一列代表一張 13x8 的影像。這個資料集的影像是 LED 數位顯示器的照片。

本練習屬於課程

Unsupervised Learning in Python

檢視課程

練習說明

  • matplotlib.pyplot 以別名 plt 匯入。
  • 選取 samples 的第 0 列並指定給 digit。例如,若要選取陣列 a 的第 2 欄,可以用 a[:,2]。請記得 samples 是 NumPy 陣列,不能使用 .loc[]iloc[] 來選取特定的列或欄。
  • 印出 digit。這已為你完成。請注意它是由 0 和 1 組成的 1D 陣列。
  • 使用 digit.reshape() 方法,取得形狀為 (13, 8) 的 2D 陣列。將結果指定給 bitmap
  • 印出 bitmap,並注意其中的 1 拼出的是數字 7!
  • 使用 plt.imshow() 函式將 bitmap 以影像形式顯示出來。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import pyplot
from matplotlib import pyplot as plt

# Select the 0th row: digit
digit = ____

# Print digit
print(digit)

# Reshape digit to a 13x8 array: bitmap
bitmap = ____

# Print bitmap
print(bitmap)

# Use plt.imshow to display bitmap
plt.____(____, cmap='gray', interpolation='nearest')
plt.colorbar()
plt.show()
編輯並執行程式碼