当前位置:   article > 正文

python层次聚类——基于sci库的代码实现和解释_from scipy.cluster.hierarchy import linkage, fclus

from scipy.cluster.hierarchy import linkage, fcluster

一、代码

  1. from scipy.cluster.hierarchy import linkage, fcluster
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. data = np.random.rand(100, 2)
  5. # 进行层次聚类(linkage返回聚类结果矩阵z)
  6. z = linkage(data, method = 'complete', metric = 'euclidean' )
  7. # 输入阈值获取聚类的结果(fcluster返回每个点所属的cluster的编号)
  8. cluster_assignments = fcluster(z, t = 0.5, criterion = 'distance')
  9. print('Cluster assignments:', cluster_assignments)
  10. # np.where根据cluster编号取点的索引
  11. clusters = [np.where(i == cluster_assignments)[0].tolist() for i in range(1, cluster_assignments.max() + 1)]
  12. print('Clusters:', clusters)
  13. # 绘制聚类结果
  14. for indices in clusters:
  15. plt.scatter(data[indices][:, 0], data[indices][:, 1])
  16. plt.show()

输出结果:

  1. Cluster assignments: [8 4 3 1 9 2 1 3 5 9 4 2 2 4 5 5 7 5 7 6 8 9 9 1 9 6 3 5 6 8 3 1 6 6 9 8 2 9 2 8 8 7 2 9 8 8 5 4 5 4 4 1 2 9 8 4 9 2 7 6 3 9 1 9 2 7 1 3 7 2 2 7 8 2 6 7 7 2 3 5 4 6 5 2 6 9 2 9 3 1 5 2 2 8 1 9 9 5 7 3]
  2. Cluster: [[3, 6, 23, 31, 51, 62, 66, 89, 94], [5, 11, 12, 36, 38, 42, 52, 57, 64, 69, 70, 73, 77, 83, 86, 91, 92], [2, 7, 26, 30, 60, 67, 78, 88, 99], [1, 10, 13, 47, 49, 50, 55, 80], [8, 14, 15, 17, 27, 46, 48, 79, 82, 90, 97], [19, 25, 28, 32, 33, 59, 74, 81, 84], [16, 18, 41, 58, 65, 68, 71, 75, 76, 98], [0, 20, 29, 35, 39, 40, 44, 45, 54, 72, 93], [4, 9, 21, 22, 24, 34, 37, 43, 53, 56, 61, 63, 85, 87, 95, 96]]
<
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号