1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| x, y_true = make_blobs(n_samples = 1000, centers = 2, cluster_std = 0.6, random_state = 8) x.shape plt.figure() plt.scatter(x[:,0], x[:,1],c=y_true,s=10, alpha=0.8) model = KMeans(n_clusters =2) model.fit(x) xmin,xmax=x[:,0].min()-1,x[:,0].max()+1 ymin,ymax=x[:,1].min()-1,x[:,1].max()+1 x1,y1=np.meshgrid(np.arange(xmin,xmax,0.1),np.arange(ymin,ymax,0.02)) z=model.predict(np.c_[x1.ravel(),y1.ravel()]) z=z.reshape(x1.shape) y_kmeans = model.predict(x) plt.figure() plt.pcolormesh(x1,y1,z,cmap=plt.cm.Pastel1,shading='auto',) plt.scatter(x[:,0], x[:,1], c = y_kmeans, cmap='Dark2', s=50, alpha=0.5, marker='x') centroids = model.cluster_centers_ plt.scatter(centroids[:,0], centroids[:,1],c=[0,1],cmap='Dark2',s=70, marker='o') plt.title('K-means 1000 points') plt.xlabel('Value1') plt.ylabel('Value2') plt.show() n=10 dis=np.zeros([n,n]) for i in range(n): distances = np.sqrt(np.sum(np.asarray(centroids[i,:] - centroids)**2, axis=1)) dis[i,:]=distances
|