K-Means聚类make_moons数据

Nabila ·
更新时间:2024-11-13
· 583 次阅读

K-Means聚类make_moons数据 题目要求: Sklearn中的make_moons方法生成数据,用K-Means聚类并可视化。输出三大指标如:ACC = 0.755, NMI = 0.1970, ARI = 0.2582。 代码示例 import matplotlib.pyplot as plt import seaborn as sns;sns.set() from sklearn.datasets import make_moons from sklearn.cluster import KMeans from sklearn.metrics import accuracy_score from sklearn.metrics import normalized_mutual_info_score from sklearn.metrics import adjusted_rand_score fig = plt.figure(1) plt.subplot(1,2,1)#一行一列 x1, y1 = make_moons(n_samples=400, noise=0.1) plt.title('make_moons function') plt.scatter(x1[:, 0], x1[:, 1], marker='o',s=15, c=y1,cmap='viridis') plt.subplot(1,2,2)#一行两列 kmeans = KMeans(n_clusters=2)#2个聚类中心 kmeans.fit(x1) y_kmeans = kmeans.predict(x1) plt.scatter(x1[:, 0], x1[:, 1], c=y_kmeans, s=15,cmap='viridis') ##三大指标 acc=accuracy_score(y1,y_kmeans) nmi=normalized_mutual_info_score(y1,y_kmeans) ari=adjusted_rand_score(y1,y_kmeans) print("ACC=",acc) print("NMI=",nmi) print("ARI=",ari) plt.show() 输出示例
在这里插入图片描述
作者:龙晨天



make k-means

需要 登录 后方可回复, 如果你还没有账号请 注册新账号