有幾種主色?
我們已經使用 matplotlib 的 image 類別中的 imread() 函式載入下列圖片。

RGB 數值已存成 DataFrame batman_df。這些 RGB 數值也已用 whiten() 函式標準化,結果分別存放在 scaled_red、scaled_blue 與 scaled_green 欄位中。
請用這個 DataFrame 繪製 elbow plot。共有幾種主色?
本練習屬於課程
Python 中的叢集分析
練習說明
- 針對
num_clusters中的每個值執行kmeans(),建立由失真值(distortion)組成的清單。 - 以清單
num_clusters與distortions建立 DataFrameelbow_plot。 - 使用
seaborn的.lineplot()繪圖,x 軸為num_clusters,y 軸為distortions。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
distortions = []
num_clusters = range(1, 7)
# Create a list of distortions from the kmeans function
for i in ____:
cluster_centers, distortion = ____
distortions.append(____)
# Create a DataFrame with two lists, num_clusters and distortions
elbow_plot = pd.DataFrame(____)
# Create a line plot of num_clusters and distortions
sns.lineplot(x=____, y=____, data = elbow_plot)
plt.xticks(num_clusters)
plt.show()