开始使用免费开始使用

有多少主色?

我们使用 matplotlibimage 类中的 imread() 函数加载了下图:

RGB 值已存入 DataFrame batman_df。这些 RGB 值已通过 whiten() 函数标准化,结果保存在 scaled_redscaled_bluescaled_green 三列中。

请使用该 DataFrame 构造肘部图。图像中有多少个主色?

本练习是课程的一部分

Python 中的聚类分析

查看课程

练习说明

  • 通过运行 kmeans() 函数,基于 num_clusters 中的每个取值生成一组畸变度(distortions)。
  • 使用列表 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()
编辑并运行代码