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

输出结果:
- 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]
- 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]]
<
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。