手写数字数据上 K-means 聚类的演示.
代码from sklearn.datasets import load_digits
from sklearn.metrics import silhouette_score
from sklearn.cluster import KMeans
def kmeans():
"""
手写数字聚类过程
:return: None
"""
# 加载数据
ld = load_digits()
print(ld.target[:20])
# 聚类
km = KMeans(n_clusters=810)
km.fit_transform(ld.data)
print(km.labels_[:20])
print(silhouette_score(ld.data, km.labels_))
return None
if __name__ == "__main__":
kmeans()
输出结果:
[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
[ 86 560 289 515 168 682 42 413 514 400 767 398 759 40 781 784 485 686
65 161]
0.0943724480183965