k-means 算法

from numpy import concatenate,column_stack,row_stack
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline
from sklearn.datasets.samples_generator import make_blobs
# X为样本特征,Y为样本簇类别, 共1000个样本,每个样本4个特征,共4个簇,簇中心在[-1,-1], [0,0],[1,1], [2,2], 簇方差分别为[0.4, 0.2, 0.2]
X, y = make_blobs(n_samples=1000,  centers=[[-1,-1], [0,0], [1,1]] ,cluster_std=[0.4, 0.2, 0.2],  random_state =9)
plt.scatter(X[:, 0], X[:, 1], marker='o')
plt.show()


from sklearn.cluster import KMeans
y_pred = KMeans(n_clusters=3, random_state=9).fit_predict(X)
plt.scatter(X[:, 0], X[:, 1], c=y_pred)
plt.show()

from sklearn import metrics
print(metrics.calinski_harabaz_score(X, y_pred))  

yy=np.array([y_pred])

un=np.hstack((X,yy.T))

print(un)

print('\n')
A_1=['0','0','0']
A_2=['1','1','1']
A_3=['2','2','2']
for i in range(yy.shape[1]):
    if un[i][2]==0:
        A_1=row_stack((A_1,un[i]))    

    elif  un[i][2]==1:
        A_2=row_stack((A_2,un[i])) 

    elif  un[i][2]==2:
        A_3=row_stack((A_3,un[i])) 


print(A_1,'\n','A_1  have ',A_1.shape[0],'element')  

print(A_2,'\n','A_2  have ',A_2.shape[0],'element')  

print(A_3,'\n','A_3 have ',A_3.shape[0],'element')

这里写图片描述

posted @ 2022-08-19 22:59  luoganttcc  阅读(1)  评论(0编辑  收藏  举报