-
Notifications
You must be signed in to change notification settings - Fork 10
/
cluster.py
101 lines (67 loc) · 2.34 KB
/
cluster.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import random
import numpy as np
from numpy import linalg as LA
import matplotlib.pyplot as plt
N=100
K=4
data1=[[0.5+0.5*random.random(),0.5+0.5*random.random()] for i in range(N)]
data2=[[0.5*random.random(),0.5*random.random()] for i in range(N)]
data1=np.array(data1)
data2=np.array(data2)
data = np.concatenate((data1,data2))
datax=data[:,0]
datay=data[:,1]
def displaydata():
plt.scatter(datax,datay)
plt.show()
assign = [[random.random(),random.random()] for k in range(K)]
assign=np.array(assign)
c0=[]
c1=[]
c2=[]
c3=[]
a=0
while a<100:
a=a+1
for i in range(len(data)):
if LA.norm(np.array(assign[0])-np.array(data[i]))<LA.norm(np.array(assign[1])-np.array(data[i])) and LA.norm(np.array(assign[0])-np.array(data[i]))<LA.norm(np.array(assign[2])-np.array(data[i])) and LA.norm(np.array(assign[0])-np.array(data[i]))<LA.norm(np.array(assign[3])-np.array(data[i])):
c0=c0+[data[i]]
elif LA.norm(np.array(assign[1])-np.array(data[i]))<LA.norm(np.array(assign[2])-np.array(data[i])) and LA.norm(np.array(assign[1])-np.array(data[i]))<LA.norm(np.array(assign[0])-np.array(data[i])) and LA.norm(np.array(assign[1])-np.array(data[i]))<LA.norm(np.array(assign[3])-np.array(data[i])):
c1=c1+[data[i]]
elif LA.norm(np.array(assign[2])-np.array(data[i]))<LA.norm(np.array(assign[0])-np.array(data[i])) and LA.norm(np.array(assign[2])-np.array(data[i]))<LA.norm(np.array(assign[1])-np.array(data[i])) and LA.norm(np.array(assign[2])-np.array(data[i]))<LA.norm(np.array(assign[3])-np.array(data[i])):
c2=c2+[data[i]]
else:
c3=c3+[data[i]]
update_assign=[[float(sum(l))/len(l) for l in zip(*c0)],[float(sum(l))/len(l) for l in zip(*c1)],[float(sum(l))/len(l) for l in zip(*c2)], [float(sum(l))/len(l) for l in zip(*c3)]]
assign= update_assign
print assign
c0x=[]
for i in range(len(c0)):
c0x=c0x+[c0[i][0]]
c0y=[]
for i in range(len(c0)):
c0y=c0y+[c0[i][1]]
c1x=[]
for i in range(len(c1)):
c1x=c1x+[c1[i][0]]
c1y=[]
for i in range(len(c1)):
c1y=c1y+[c1[i][1]]
c2x=[]
for i in range(len(c2)):
c2x=c2x+[c2[i][0]]
c2y=[]
for i in range(len(c2)):
c2y=c2y+[c2[i][1]]
c3x=[]
for i in range(len(c3)):
c3x=c3x+[c3[i][0]]
c3y=[]
for i in range(len(c3)):
c3y=c3y+[c3[i][1]]
fig, ax=plt.subplots()
ax.scatter(c0x,c0y,color='r')
ax.scatter(c1x,c1y,color='g')
ax.scatter(c2x,c2y,color='b')
ax.scatter(c3x,c3y,color='y')
plt.show()