层次聚类:single 方法
让我们继续使用相同的 footfall 数据集,看看换一种聚类方法后是否会有变化。
数据存放在 pandas 的 DataFrame comic_con 中。x_scaled 和 y_scaled 是在某一时刻人群的 X、Y 坐标经过标准化后的列名。
本练习是课程的一部分
Python 中的聚类分析
练习说明
- 从
scipy.cluster.hierarchy导入fcluster和linkage。 - 在
linkage()函数中使用single方法。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import the fcluster and linkage functions
from ____ import ____, ____
# Use the linkage() function
distance_matrix = ____(comic_con[[____, ____]], ____ = ____, metric = ____)
# Assign cluster labels
comic_con['cluster_labels'] = ____(____, ____, ____)
# Plot clusters
sns.scatterplot(x='x_scaled', y='y_scaled',
hue='cluster_labels', data = comic_con)
plt.show()