-
Notifications
You must be signed in to change notification settings - Fork 0
/
k-means(auto-cease)
137 lines (119 loc) · 3.08 KB
/
k-means(auto-cease)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from random import *
from matplotlib import pyplot
rand = Random()
rand.seed(0)
vari = 0.2
Step_num = 20
x1num = 100
x1 = [rand.normalvariate(2, vari) for i in range(0,x1num)]
y1 = [rand.normalvariate(2, vari) for i in range(0,x1num)]
l1 = [0 for i in range(0,x1num)]
x2num = 100
x2 = [rand.normalvariate(1, vari) for i in range(0,x2num)]
y2 = [rand.normalvariate(1, vari) for i in range(0,x2num)]
l2 = [1 for i in range(0,x2num)]
x3num = 100
x3 = [rand.normalvariate(1, vari) for i in range(0,x3num)]
y3 = [rand.normalvariate(2, vari) for i in range(0,x3num)]
l3 = [2 for i in range(0,x3num)]
x = x1 + x2 + x3
y = y1 + y2 + y3
label = l1 + l2 +l3
total = x1num + x2num + x3num
a = [0.1,0.2,0.3] #初始点/质心点
b = [1.5,1.6,1.7]
colors = ['red','green','blue', 'yellow']
demo_colors = ['red','green','blue']
data_colors = [colors[l] for l in label]
pyplot.scatter(x, y,color=data_colors, alpha=0.7)
pyplot.scatter(a, b,color=demo_colors,s=200, alpha=0.7)
pyplot.show()
pre_t = []
t = []#辅助数组
def isready(t,pre_t):
for i in range (0,total):
print(t[i])
print(pre_t[i])
if (t[i]!=pre_t[i]):
return 0
else:
continue
pre_t = t
return 1
for i in range (0,total):
pre_t.append(label[i])
while (1):
t = []
redx = []
redy = []
greenx = []
greeny = []
bluex = []
bluey = []
#找到最近的点
for i in range (0,total):
color = ''
dist=[]
for j in range (0,3):
dist.append((x[i]-a[j])*(x[i]-a[j])+(y[i]-b[j])*(y[i]-b[j]))
dis = min(dist[0],dist[1],dist[2])
for j in range (0,3):
if dis == dist[j]:
key = j
if(key == 0):
redx.append(x[i])
redy.append(y[i])
elif key == 1:
greenx.append(x[i])
greeny.append(y[i])
else:
bluex.append(x[i])
bluey.append(y[i])
color = demo_colors[key]
t.append(key)
#重新确定质心
a =[]
b =[]
s1x=0
s1y=0
s2x=0
s2y=0
s3x=0
s3y=0
for i in range(len(redx)):
s1x+=redx[i]
s1y+=redy[i]
avg1x = s1x/len(redx)
avg1y = s1y/len(redx)
for i in range(len(greenx)):
s2x+=greenx[i]
s2y+=greeny[i]
avg2x = s2x/len(greenx)
avg2y = s2y/len(greenx)
for i in range(len(bluex)):
s3x+=bluex[i]
s3y+=bluey[i]
avg3x = s3x/len(bluex)
avg3y = s3y/len(bluex)
a.append(avg1x)
b.append(avg1y)
a.append(avg2x)
b.append(avg2y)
a.append(avg3x)
b.append(avg3y)
pyplot.scatter(a, b,color=demo_colors,s=200, alpha=0.7)
pyplot.scatter(redx, redy,color='red', alpha=0.7)
pyplot.scatter(bluex, bluey,color='blue', alpha=0.7)
pyplot.scatter(greenx, greeny,color='green', alpha=0.7)
pyplot.show()
flag = True#flag true -> end
for i in range (0,total):
if (t[i]!=pre_t[i]):
flag = False
break
else:
continue
if (flag):
break
else:
pre_t= t