andyKong

导航

2D K-means algorithem python code

K-means算法的尝试,以随机生成的两簇点集作为例子。初学的原因,代码比较粗糙,以后有待优化。


 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Fri Mar  9 09:16:45 2018
 4 
 5 @author: user
 6 """
 7 import numpy as np
 8 import matplotlib.pyplot as plt
 9 import math
10 #generate random points in two clusters
11 x1=2+1.3*np.random.randn(55)
12 y1=3+2*np.random.randn(55)
13 x2=7+1.5*np.random.randn(55)
14 y2=7+1.3*np.random.randn(55)
15 x1=np.concatenate((x1,y1));x2=np.concatenate((x2,y2));
16 x1=x1.reshape(int(0.5*len(x1)),2);x2=x2.reshape(int(0.5*len(x2)),2)
17 x=np.concatenate((x1,x2))
18 x=list(x)
19 #define distance between two point:
20 def distance(x1,x2):
21     return math.sqrt(pow((x1[0]-x2[0]),2)+pow(x1[1]-x2[1],2))
22 #initialization ,show original points
23 u=[[10,2],[2,8]];r=[];x1=[];x2=[];a=b=c=d=0;
24 fig = plt.figure();
25 x=np.array(x)
26 plt.scatter(x[:,0],x[:,1],marker='x')
27 plt.xlim(-1,12);plt.ylim(-1,12)
28 plt.show()
29 #M-step
30 for z in range(1,6):
31     for i in range(len(x)):
32         if distance(x[i],u[0]) < distance(x[i],u[1]):
33             r.append(1);
34         else:
35             r.append(0);
36         if r[i]==1:
37             x1.append(x[i]);
38         else:
39             x2.append(x[i]);
40    
41     for j in range(len(x1)):
42         a = a + x1[j][0];b = b + x1[j][1]
43     u[0][0] = a / len(x1);u[0][1] = b / len(x1)
44     for k in range(len(x2)):
45        c = c + x2[k][0];d = d + x2[k][1]
46     #E-step47   u[1][0] = c / len(x2);u[1][1] = d / len(x2)
48     x1=np.array(x1);x2=np.array(x2);
49     plt.scatter(x1[:,0],x1[:,1],c='b',marker='x')
50     plt.scatter(x2[:,0],x2[:,1],c='r',marker='x')
51 
52     plt.xlim(-1,12);plt.ylim(-1,12)
53     r=[];x1=[];x2=[];a=b=c=d=0;
54 
55     if z==5:
56             plt.scatter(u[0][0],u[0][1],c='g',marker='s')
57             plt.scatter(u[1][0],u[1][1],c='y',marker='s')
58             plt.show()
59     else: pass

 

 

 

posted on 2018-03-14 00:15  andyKong  阅读(136)  评论(0)    收藏  举报