開始使用免費開始

有幾種主色?

我們已經使用 matplotlibimage 類別中的 imread() 函式載入下列圖片。

RGB 數值已存成 DataFrame batman_df。這些 RGB 數值也已用 whiten() 函式標準化,結果分別存放在 scaled_redscaled_bluescaled_green 欄位中。

請用這個 DataFrame 繪製 elbow plot。共有幾種主色?

本練習屬於課程

Python 中的叢集分析

檢視課程

練習說明

  • 針對 num_clusters 中的每個值執行 kmeans(),建立由失真值(distortion)組成的清單。
  • 以清單 num_clustersdistortions 建立 DataFrame elbow_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()
編輯並執行程式碼