DIANA鸢尾花聚类可视化python代码并输出轮廓系数
时间: 2023-09-08 12:07:35 浏览: 147
Diana.rar_DIANA算法_层次 聚类_层次聚类diana_聚类算法diana
5星 · 资源好评率100%
好的,以下是DIANA鸢尾花聚类可视化的Python代码,并输出轮廓系数:
```python
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt
%matplotlib inline
# 加载数据
iris = load_iris()
data = pd.DataFrame(iris.data, columns=iris.feature_names)
# 计算距离矩阵
dist_matrix = pdist(data, metric='euclidean')
# 进行聚类
linkage_matrix = linkage(dist_matrix, method='ward')
# 绘制聚类树状图
plt.figure(figsize=(12, 6))
dendrogram(linkage_matrix)
plt.title('Dendrogram')
plt.xlabel('Index')
plt.ylabel('Distance')
plt.show()
# 计算轮廓系数
labels = linkage(linkage_matrix, method='ward', optimal_ordering=True)['leaves']
silhouette_avg = silhouette_score(data, labels)
print('轮廓系数为', silhouette_avg)
```
注:以上代码使用了`scipy`和`sklearn`两个库,需要提前安装。
阅读全文