其他分享
首页 > 其他分享> > sklearn-Kmeans

sklearn-Kmeans

作者:互联网

Kmeans,使用sklearn实现

 1 from sklearn.cluster import KMeans
 2 import numpy as np
 3 X = np.array([[1, 2], [1, 4], [1, 0],
 4                    [10, 2], [10, 4], [10, 0]])
 5 kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
 6 labels = kmeans.labels_  # 标签,默认从0开始
 7 centers = kmeans.cluster_centers_ # 聚簇中心
 8 print('labels:',labels)
 9 print('centers:',centers)
10 print('----------------')
11 print(centers[labels])  # 将每个点对应的聚簇中心打印出来

打印结果:

labels: [1 1 1 0 0 0]
centers: [[10. 2.]
[ 1. 2.]]
----------------
[[ 1. 2.]
[ 1. 2.]
[ 1. 2.]
[10. 2.]
[10. 2.]
[10. 2.]]

标签:10,labels,Kmeans,kmeans,print,centers,sklearn
来源: https://www.cnblogs.com/shuangcao/p/12171917.html