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)
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()
- 输出示例