顯示主要色彩
我們已使用 matplotlib 的 image 類別裡的 imread() 函式載入下列影像。

若要顯示主要色彩,請先把叢集中心的色彩從標準化值轉回原始值,接著再換算到 0-1 的範圍,使用下列公式:
converted_pixel = standardized_pixel * pixel_std / 255
RGB 數值已存放在 DataFrame batman_df 中。縮放後的 RGB 數值分別位於 scaled_red、scaled_blue 和 scaled_green 欄。叢集中心則存放在變數 cluster_centers,這些是使用 kmeans() 函式、分成 3 個叢集所產生的。
本練習屬於課程
Python 中的叢集分析
練習說明
- 從 DataFrame 取得每個色彩的標準差,並分別存到
r_std、g_std、b_std。 - 對每個叢集中心,將標準化後的 RGB 值轉換為 0-1 範圍內的縮放值。
- 顯示各叢集中心所對應的色彩。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Get standard deviations of each color
____, ____, ____ = batman_df[['red', 'green', 'blue']].___()
for cluster_center in cluster_centers:
scaled_r, scaled_g, scaled_b = cluster_center
# Convert each standardized value to scaled value
colors.append((
scaled_r * ____ / ____,
scaled_g * ____ / ____,
scaled_b * ____ / ____
))
# Display colors of cluster centers
plt.____(____)
plt.show()