有多少主色?
我们使用 matplotlib 的 image 类中的 imread() 函数加载了下图:

RGB 值已存入 DataFrame batman_df。这些 RGB 值已通过 whiten() 函数标准化,结果保存在 scaled_red、scaled_blue 和 scaled_green 三列中。
请使用该 DataFrame 构造肘部图。图像中有多少个主色?
本练习是课程的一部分
Python 中的聚类分析
练习说明
- 通过运行
kmeans()函数,基于num_clusters中的每个取值生成一组畸变度(distortions)。 - 使用列表
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()