-
Notifications
You must be signed in to change notification settings - Fork 10
/
cluster2.py
77 lines (52 loc) · 1.33 KB
/
cluster2.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
import random
import numpy as np
from numpy import linalg as LA
import matplotlib.pyplot as plt
data1=[]
for i in range(100):
data1=data1+[[0.5+0.5*random.random(),0.5+0.5*random.random()]]
data2=[]
for i in range(100):
data2=data2+[[0.5*random.random(),0.5*random.random()]]
data=data1+data2
print len(data)
datax=[]
for i in range(len(data)):
datax=datax+[data[i][0]]
datay=[]
for i in range(len(data)):
datay=datay+[data[i][1]]
plt.scatter(datax,datay)
plt.show()
assign = []
l=2
ctensor=[]
ctensorx=[]
ctensory=[]
update_assign=[]
for j in range(l):
assign=assign+[[random.random(),random.random()]]
ctensor=ctensor+[[]]
ctensorx=ctensorx+[[]]
ctensory=ctensory+[[]]
update_assign=update_assign+[[]]
turn=0
while turn<100:
turn=turn+1
for k in range(l):
lowest = 2
for i in range(len(data)):
while LA.norm(np.array(assign[k])-np.array(data[i]))<lowest:
lowest = LA.norm(np.array(assign[k])-np.array(data[i]))
ctensor[k]=ctensor[k]+[data[i]]
for j in range(len(ctensor[k])):
ctensorx[k]= ctensorx[k]+[ctensor[k][j][0]]
ctensory[k]= ctensory[k]+[ctensor[k][j][1]]
update_assign[k]=[np.mean(ctensorx[k]),np.mean(ctensory[k])]
assign= update_assign
print assign
colors=['r','g']
fig, ax=plt.subplots()
for m in range(l):
ax.scatter(ctensorx[m],ctensory[m],color=colors[m])
plt.show()