开始使用免费开始使用

显示主色调

我们已经使用 matplotlibimage 类中的 imread() 函数加载了如下图片。

要显示主色调,请将聚类中心的颜色先转换为原始值,然后依据下式转换到 0-1 范围: converted_pixel = standardized_pixel * pixel_std / 255

RGB 值存储在 DataFrame batman_df 中。缩放后的 RGB 值存放在 scaled_redscaled_bluescaled_green 列中。聚类中心存放在变量 cluster_centers 中,是使用 kmeans() 函数并设置 3 个簇得到的。

本练习是课程的一部分

Python 中的聚类分析

查看课程

练习说明

  • 从 DataFrame 中获取每种颜色的标准差,并分别存入 r_stdg_stdb_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()
编辑并运行代码