使用sklearn中的聚类评估指标评估kmeans和DBSCAN并可视化
时间: 2024-05-08 12:17:27 浏览: 122
以下是在sklearn中使用聚类评估指标评估Kmeans和DBSCAN并可视化的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans, DBSCAN
from sklearn import datasets
from sklearn.metrics import silhouette_score, calinski_harabasz_score, davies_bouldin_score
# 加载数据集
iris = datasets.load_iris()
X = iris.data
# KMeans聚类
kmeans = KMeans(n_clusters=3, random_state=0).fit(X)
labels_kmeans = kmeans.labels_
# DBSCAN聚类
dbscan = DBSCAN(eps=0.5, min_samples=5).fit(X)
labels_dbscan = dbscan.labels_
# 可视化聚类结果
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.scatter(X[:, 0], X[:, 1], c=labels_kmeans)
plt.title('KMeans Clustering')
plt.subplot(1, 2, 2)
plt.scatter(X[:, 0], X[:, 1], c=labels_dbscan)
plt.title('DBSCAN Clustering')
plt.show()
# 使用轮廓系数评估KMeans和DBSCAN聚类
score_kmeans = silhouette_score(X, labels_kmeans)
score_dbscan = silhouette_score(X, labels_dbscan)
print('Silhouette Score of KMeans:', score_kmeans)
print('Silhouette Score of DBSCAN:', score_dbscan)
# 使用Calinski-Harabasz指数评估KMeans和DBSCAN聚类
score_kmeans = calinski_harabasz_score(X, labels_kmeans)
score_dbscan = calinski_harabasz_score(X, labels_dbscan)
print('Calinski-Harabasz Score of KMeans:', score_kmeans)
print('Calinski-Harabasz Score of DBSCAN:', score_dbscan)
# 使用Davies-Bouldin指数评估KMeans和DBSCAN聚类
score_kmeans = davies_bouldin_score(X, labels_kmeans)
score_dbscan = davies_bouldin_score(X, labels_dbscan)
print('Davies-Bouldin Score of KMeans:', score_kmeans)
print('Davies-Bouldin Score of DBSCAN:', score_dbscan)
```
在此示例中,我们使用了Iris数据集,并使用KMeans和DBSCAN进行聚类。然后,我们使用轮廓系数、Calinski-Harabasz指数和Davies-Bouldin指数来评估聚类质量。最后,我们还可视化了聚类结果。
阅读全文