from sklearn.datasets import make_classification from sklearn.cluster import KMeans, DBSCAN # 生成模拟数据 X, y = make_classification(n_samples=100, n_features=20, n_informative=2, n_redundant=2, n_clusters_per_class=2, random_state=42) # 使用KMeans算法进行聚类 kmeans = KMeans(n_clusters=2, random_state=42) kmeans_labels = kmeans.fit_predict(X) # 使用DBSCAN算法进行聚类 dbscan = DBSCAN(eps=1.0, min_samples=5) dbscan_labels = dbscan.fit_predict(X) # 输出聚类结果 print("KMeans聚类结果:", kmeans_labels) print("DBSCAN聚类结果:", dbscan_labels),要代码
时间: 2024-03-15 13:44:00 浏览: 140
python(sklearn)
以下是您所述的代码:
```python
from sklearn.datasets import make_classification
from sklearn.cluster import KMeans, DBSCAN
# 生成模拟数据
X, y = make_classification(n_samples=100, n_features=20, n_informative=2, n_redundant=2, n_clusters_per_class=2, random_state=42)
# 使用KMeans算法进行聚类
kmeans = KMeans(n_clusters=2, random_state=42)
kmeans_labels = kmeans.fit_predict(X)
# 使用DBSCAN算法进行聚类
dbscan = DBSCAN(eps=1.0, min_samples=5)
dbscan_labels = dbscan.fit_predict(X)
# 输出聚类结果
print("KMeans聚类结果:", kmeans_labels)
print("DBSCAN聚类结果:", dbscan_labels)
```
阅读全文