探索 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()