from sklearn.datasets import make_blobs from sklearn.cluster import AgglomerativeClustering import numpy as np import matplotlib.pyplot as plt from itertools import cycle centers=[[1,1],[-1,-1],[1,-1]] n_samples = 3000 X, lables_true = make_blobs(n_samples = n_samples, centers= centers, cluster_std = 0.6,random_state = 0) linkages = ['ward', 'average', 'complete','single']\ for i, v in enumerate(linkage): n_clusters_=3 #构建凝聚层次类模型,参数linkage依次取值linkages中的各个元素 , n_clusters=n_clusters,其他参数默认 ac= #训练数据集 #获取聚类数据在分类,赋值给lables lables= #结果数据可视化 #使用subplot构建2行2列的图画布局 plt.subplot #绘制单个层次聚类结果图 plt.scatter plt.title('%s,%d'%(v,ac.n_clusters)) #思考标题中两个值分别表示什么意义 plt.show()
时间: 2023-07-16 09:15:59 浏览: 191
这段代码使用Scikit-learn库中的make_blobs函数生成了一个包含3个中心的随机数据集,每个中心周围有高斯分布的数据点。然后使用AgglomerativeClustering函数进行层次聚类,参数linkage依次取值linkages中的各个元素,n_clusters设置为3。最后使用matplotlib库绘制聚类结果图。具体解释请看注释:
```python
from sklearn.datasets import make_blobs
from sklearn.cluster import AgglomerativeClustering
import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle
centers = [[1, 1], [-1, -1], [1, -1]]
n_samples = 3000
# 生成随机数据集
X, labels_true = make_blobs(n_samples=n_samples, centers=centers, cluster_std=0.6, random_state=0)
linkages = ['ward', 'average', 'complete', 'single']
# 构建4种不同链接方式的凝聚层次聚类模型
for i, linkage in enumerate(linkages):
n_clusters_ = 3
# 构建凝聚层次类模型,参数linkage依次取值linkages中的各个元素,n_clusters=n_clusters,其他参数默认
ac = AgglomerativeClustering(linkage=linkage, n_clusters=n_clusters_)
# 训练数据集
labels = ac.fit_predict(X)
# 结果数据可视化
# 使用subplot构建2行2列的图画布局
plt.subplot(2, 2, i + 1)
# 绘制单个层次聚类结果图
colors = cycle('bgrcmyk')
for k, col in zip(range(n_clusters_), colors):
my_members = labels == k
plt.scatter(X[my_members, 0], X[my_members, 1], color=col, label='Cluster %d' % k, alpha=0.5)
plt.title('%s, %d' % (linkage, ac.n_clusters))
plt.show()
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)