层次聚类 Complete Link (MAX) python代码
时间: 2023-07-23 11:07:10 浏览: 52
以下是使用scipy库实现Complete Link (MAX)层次聚类的Python代码示例:
```python
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt
# 样本数据
X = [[1, 2], [2, 1], [2, 4], [3, 3], [5, 4], [5, 6], [6, 5], [7, 7]]
# 完全连接聚类
Z = linkage(X, 'complete')
# 层次聚类树状图
plt.figure(figsize=(10, 5))
plt.title('Hierarchical Clustering Dendrogram (Complete Link)')
plt.xlabel('Sample Index')
plt.ylabel('Distance')
dendrogram(Z, leaf_rotation=90.0, leaf_font_size=8.0)
plt.show()
```
在这个例子中,我们使用样本数据X进行完全连接聚类,并使用matplotlib库绘制了层次聚类树状图。其中,linkage函数的第二个参数指定了聚类算法,此处为'complete'表示使用完全连接聚类。最后一个参数dendrogram用于绘制树状图。
相关问题
层次聚类Single Link (MIN) python代码
下面是一个基于Python的单链接(Single Link)层次聚类算法的示例代码:
```python
import numpy as np
def single_linkage(X, k):
"""
Single Linkage Clustering
X: ndarray, shape(n_samples, n_features)
k: int, the number of clusters
"""
n_samples = X.shape[0]
# initialize the distance matrix
dist_matrix = np.zeros((n_samples, n_samples))
for i in range(n_samples):
for j in range(i+1, n_samples):
dist_matrix[i, j] = np.linalg.norm(X[i] - X[j])
dist_matrix[j, i] = dist_matrix[i, j]
# initialize the clusters
clusters = [[i] for i in range(n_samples)]
# merge clusters iteratively
while len(clusters) > k:
# find the closest pair of clusters
min_dist = np.inf
for i in range(len(clusters)):
for j in range(i+1, len(clusters)):
dist = np.min(dist_matrix[np.ix_(clusters[i], clusters[j])])
if dist < min_dist:
min_dist = dist
merge_pair = (i, j)
# merge the two closest clusters
new_cluster = clusters[merge_pair[0]] + clusters[merge_pair[1]]
clusters.pop(merge_pair[1])
clusters[merge_pair[0]] = new_cluster
# update the distance matrix
for i in range(len(clusters)-1):
dist_matrix[i, merge_pair[0]] = np.min(dist_matrix[np.ix_(clusters[i], new_cluster)])
dist_matrix[merge_pair[0], i] = dist_matrix[i, merge_pair[0]]
dist_matrix[merge_pair[0], merge_pair[0]] = np.inf
return clusters
```
其中,输入参数`X`是数据集,`k`是期望的聚类数。`dist_matrix`是距离矩阵,用来记录每个样本之间的距离。`clusters`是一个列表,每个元素是一个聚类簇的索引列表。在每次迭代中,找到距离最近的一对聚类簇,将它们合并成一个新的聚类簇,并更新距离矩阵。最终返回聚类结果。
层次聚类 python代码
层次聚类是一种基于树形结构的聚类算法,它通过计算样本之间的相似度来构建一棵树形结构,树的叶子节点就是样本,而树的根节点则是所有样本的最近公共祖先。层次聚类分为两种:自上而下的聚合式层次聚类和自下而上的分裂式层次聚类。
下面是一个使用 Python 实现层次聚类的简单示例代码:
```python
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt
# 生成随机数据
X = np.random.rand(10, 2)
# 计算相似度矩阵
Z = linkage(X, 'ward')
# 绘制树状图
fig = plt.figure(figsize=(25, 10))
dn = dendrogram(Z)
plt.show()
```
在这个示例中,我们使用 numpy 库生成了一个包含 10 个样本,每个样本有两个特征的随机数据集。然后使用 scipy 库中的 linkage 函数计算相似度矩阵,并选择使用 Ward 方法进行层次聚类。最后使用 matplotlib 库绘制树状图。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)