#
http://blog.csdn.net/github_36326955/article/details/54999627
#-*-coding:utf-8-*-ImportNumPy as NPImportMatplotlib.pyplot as Plt fromSklearn.datasets.samples_generatorImportMake_blobsImportSklearnx,y= Make_blobs (n_samples=1000,n_features=2,centers=[[-1,-1],[0,0],[1,1],[2,2]],cluster_std=[0.4,0.2,0.2,0.2], Random_state=9) Plt.scatter (x[:,0],x[:,1],marker='o') plt.show () fromSklearn.clusterImportkmeansy_pred= Kmeans (n_clusters=2,random_state=9). Fit_predict (X) plt.scatter (x[:,0],x[:,1],c=y_pred) plt.show () fromSklearnImportMetricsPrint(Metrics.calinski_harabaz_score (x,y_pred)) y_pred= Kmeans (n_clusters=3,random_state=9). Fit_predict (X) plt.scatter (x[:,0],x[:,1],c=y_pred) plt.show ()Print(Metrics.calinski_harabaz_score (x,y_pred)) y_pred= Kmeans (n_clusters=4,random_state=9). Fit_predict (X) plt.scatter (x[:,0],x[:,1],c=y_pred) plt.show ()Print(Metrics.calinski_harabaz_score (x,y_pred)) fromSklearn.clusterImportMinibatchkmeans#Minibatchkmeans forIndex,valinchEnumerate ((2,3,4,5)): Plt.subplot (2,2,index+1) y_pred= Minibatchkmeans (N_clusters=val, batch_size=200,random_state=9). Fit_predict (X) score=Metrics.calinski_harabaz_score (x,y_pred) plt.scatter (x[:,0],x[:,1],c=y_pred) Plt.text (0.99,0.01, ('val=%d, score:%.2f'% (Val,score)), TRANSFORM=PLT.GCA (). transaxes,size=10, HorizontalAlignment=' Right') plt.show ()
Python code implementations are available for reference:
http://blog.csdn.net/dream_angel_z/article/details/46343597
Python---sklearn---kmeans