k-means 算法

来源:互联网 发布:京东联盟和淘宝联盟 编辑:程序博客网 时间:2024/06/05 14:14
from numpy import concatenate,column_stack,row_stackimport numpy as npimport matplotlib.pyplot as plt#%matplotlib inlinefrom 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 KMeansy_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 metricsprint(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')

这里写图片描述

原创粉丝点击