加载中...
KMeans
发表于:2021-11-08 | 分类: 机器学习课程(魏)
1
2
3
4
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
import numpy as np
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#创建数据
#make_blobs 聚类生成器
x, y_true = make_blobs(n_samples = 1000, #生成1000条数据
centers =5, #5类数据
cluster_std = 0.5, #方差一致
random_state = 9) #随机数种子
#x.shape #(300, 2)
plt.figure()
plt.scatter(x[:,0], x[:,1])

model = KMeans(n_clusters=5)#n_clusters是k
model.fit(x)
y_kmeans = model.predict(x)
plt.figure()
plt.scatter(x[:,0], x[:,1], c = y_kmeans, cmap='Dark2', s=50, alpha=0.5, marker='o')

centroids = model.cluster_centers_
plt.scatter(centroids[:,0], centroids[:,1],c=[0,1,2,3,4],cmap='Dark2',s=250, marker='*')
plt.title('K-means 1000 points')
plt.xlabel('Value1')
plt.ylabel('Value2')
plt.show()


png

png

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, #生成300条数据
centers = 2, #5类数据
cluster_std = 0.6, #方差一致
random_state = 8) #随机数种子
x.shape #(300, 2)
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


png

png

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-6-73969d6b1829> in <module>
     27 for i in range(n):
     28     distances = np.sqrt(np.sum(np.asarray(centroids[i,:] - centroids)**2, axis=1))
---> 29     dis[i,:]=distances


ValueError: could not broadcast input array from shape (2) into shape (10)
1

上一篇:
第一讲 神经网络计算
下一篇:
KNN
本文目录
本文目录