from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
X, y=make_blobs(n_samples=600, n_features=2, centers=4, random_state=0)
n_clusters=4
cluster=KMeans(n_clusters=n_clusters, max_iter=100, init='random')
y_pred =cluster.fit_predict(X)
#查看标签
y_pred=cluster.labels_
#查看簇心点坐标
centers =cluster.cluster_centers_
#簇心的均方距离
cluster.inertia_
#循环次数
cluster.n_iter_
for i in range(n_clusters):
plt.scatter(X[y_pred==i,0], X[y_pred==i,1])
plt.scatter(centers[:,0], centers[:,1], marker='^' ,color='purple')
plt.show()
轮廓系数 最好=1,最差=-1
from sklearn.metrics import silhouette_samples, silhouette_score
#返回全部样本的轮廓系数均值
silhouette_score(X,y_pred)
#返回每个样本的轮廓系数
silhouette_samples(X, y_pred)
k_means会返回 ,cluster.cluster_centers_,cluster.labels_,cluster.inertia_,cluster.n_iter_
from sklearn.cluster import k_means
k_means(X, n_cluster, return_n_iter=True)
网友评论