dbscan实现iris鸢尾花数据聚类的代码
时间: 2023-05-10 12:55:34 浏览: 263
k均值、合并聚类和DBSCAN聚类算法对鸢尾花数据集聚类代码.zip
5星 · 资源好评率100%
以下是使用Python实现DBSCAN算法对Iris鸢尾花数据进行聚类的代码:
```python
from sklearn.datasets import load_iris
from sklearn.cluster import DBSCAN
import numpy as np
# 加载数据
iris = load_iris()
X = iris.data
# DBSCAN聚类
dbscan = DBSCAN(eps=0.5, min_samples=5)
dbscan.fit(X)
# 输出聚类结果
labels = dbscan.labels_
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
print('Estimated number of clusters: %d' % n_clusters_)
print("Homogeneity: %0.3f" % metrics.homogeneity_score(iris.target, labels))
print("Completeness: %0.3f" % metrics.completeness_score(iris.target, labels))
print("V-measure: %0.3f" % metrics.v_measure_score(iris.target, labels))
print("Adjusted Rand Index: %0.3f"
% metrics.adjusted_rand_score(iris.target, labels))
print("Adjusted Mutual Information: %0.3f"
% metrics.adjusted_mutual_info_score(iris.target, labels))
print("Silhouette Coefficient: %0.3f"
% metrics.silhouette_score(X, labels))
```
注意:这里的eps和min_samples是DBSCAN算法的两个重要参数,需要根据具体数据进行调整。
阅读全文