代码如下:
from sklearn import datasets
from sklearn.cluster import KMeans
#导入数据
iris = datasets.load_iris()
x = iris.data
y = iris.target
#构建并训练K均值模型
kmeans = KMeans(n_clusters=3,random_state=0).fit(x)
print('K均值模型为:\n',kmeans)
import matplotlib.pyplot as plt
#获取模型聚类结果
y_pre = kmeans.predict(x)
#绘制iris原本的类别
plt.scatter(x[:,0],x[:,1],c = y)
plt.show()
#绘制K-Means聚类结果
plt.scatter(x[:,0],x[:,1],c = y_pre)
plt.show()
from sklearn.metrics import jaccard_similarity_score,fowlkes_mallows_score,adjusted_rand_score,davies_bouldin_score
print('K均值类模型的Jaccard系数:',jaccard_similarity_score(y,y_pre))
print('K均值类模型的FM系数:',fowlkes_mallows_score(y,y_pre))
print('K均值类模型的调整Rand系数:',adjusted_rand_score(y,y_pre))
print('K均值类模型的DB系数:',davies_bouldin_score(x,kmeans.labels_))
结果如下: