-
Notifications
You must be signed in to change notification settings - Fork 3
/
KMean++.py
275 lines (255 loc) · 8.74 KB
/
KMean++.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# -*- coding: utf-8 -*-
import numpy
import math
from random import choice
from copy import copy
from time import time
import pickle
import re
r='[’!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~]+'
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
FLOAT_MAX = 1e100
from dA import test_x_y_dA
class Point:
__slots__ = ["Sent", "group","SentVec","number"]
def __init__(self, Sent=None, group=0,number=0,SentVec=None):
self.Sent, self.group ,self.SentVec,self.number= Sent,group,SentVec,number
#通过RAD将每个句子的词向量计算出来
def points_Vec_Rad(file_sent):
MyVec={}
MyNumber={}
#将存储的词向量模型取出
pkl_file=open('data/user_vec.pkl', 'r')
data=pickle.load(pkl_file)
pkl_file.close()
#read sentence
f = open(file_sent,'r')
for mysent in f:
line=re.sub(r,"",mysent.split('. ')[0])
# print line
word_Seq=line.split(' ')
# print(word_Seq)
pipei_y=list(data[word_Seq[0]])#first word
for word in word_Seq:
wordVec_Two_Merge=numpy.array(list(pipei_y)+list(data[word]))#合并两个词向量
pipei_y=test_x_y_dA(wordVec_Two_Merge)
MyVec[line]=pipei_y
#构造句子和句子number的字典
number=mysent.split('. ')[1].strip('\n')
MyNumber[line]=number
# print number
f.close()
#存储句子和向量的字典数据
fout=open(filename,'w')
pickle.dump(MyVec,fout)
pickle.dump(MyNumber,fout)
fout.close()
#不做任何处理将每个句子的词向量计算出来
def points_Vec_Add(file_sent):
MyNumber={}
MyVec={}
#将存储的词向量模型取出
pkl_file=open('data/user_vec.pkl', 'r')
data=pickle.load(pkl_file)
pkl_file.close()
#读取句子
f = open(file_sent,'r')
for mysent in f:
#计算句子的向量
line=re.sub(r,"",mysent.split('. ')[0])
# print line
word_Seq=line.split(' ')
# print(word_Seq)
SUM_VEC=numpy.array(data[word_Seq[0]])
for word in word_Seq:
SUM_VEC =numpy.array(SUM_VEC+numpy.array(data[word]))#add two word vector 有待提高
MyVec[line]=SUM_VEC
# print SUM_VEC
#构造句子和句子number的字典
number=mysent.split('. ')[1].strip('\n')
MyNumber[line]=number
# print number
f.close()
#存储句子和向量的字典数据
fout=open(filename,'w')
pickle.dump(MyVec,fout)
pickle.dump(MyNumber,fout)
fout.close()
def generate_points():
"""将文件里面的句子读入point集里面"""
pkl_file=open(filename,'r')
data = pickle.load(pkl_file)
labelNumber=pickle.load(pkl_file)
pkl_file.close()
MySent=data.keys()
points = [Point() for _ in xrange(len(data))]
for i in xrange(len(data)):
points[i].Sent = MySent[i]
points[i].SentVec =data[MySent[i]]
points[i].number = labelNumber[MySent[i]]
return points
def similarity(w1, w2):
"""计算词向量的相似度,使用余弦"""
vec = numpy.dot(w1, w2)
veclen=numpy.sqrt(numpy.sum(numpy.array(w1)**2))*numpy.sqrt(numpy.sum(numpy.array(w2)**2))
if veclen>0.0:
return vec / veclen
else:
print 'error vec'
return ('error vec')
def nearest_cluster_center(point, cluster_centers):
"""计算每个点到聚类中心的距离,返回每个点的最近的聚类中心和距离"""
min_index = point.group
min_dist = FLOAT_MAX
for i, cc in enumerate(cluster_centers):
if cc.Sent == None:
continue
else:
d = 1-similarity(cc.SentVec, point.SentVec)
if min_dist > d:
min_dist = d
min_index = i
return (min_index, min_dist)
#选择批次距离尽可能远的K个点
def K_findseed(points, cluster_centers):
"""初始化聚类中心,并将点分到这些聚类中心"""
cluster_centers[0] = copy(choice(points))
d = [0.0 for _ in xrange(len(points))]
for i in xrange(1, len(cluster_centers)):
for j, p in enumerate(points):
d[j] = nearest_cluster_center(p, cluster_centers[:i])[1]
max=d[0];index_max=0
for j, di in enumerate(d):
if(di>max):
max=di
index_max=j
else:
continue
cluster_centers[i] = copy(points[index_max])
cluster_centers[i].group=i
for p in points:
p.group = nearest_cluster_center(p, cluster_centers)[0]
#initialize K_centre_point
def K_findseed_1(points, cluster_centers):
"""初始化聚类中心,并将点分到这些聚类中心"""
for i in xrange(0, len(cluster_centers)):
cluster_centers[i] = copy(choice(points))
cluster_centers[i].group=i
print cluster_centers[i].Sent
for p in points:
p.group = nearest_cluster_center(p, cluster_centers)[0]
def comp_cluster_centers(Cluster_centre):
"""计算聚类中心的向量"""
Cluster_centre_matrix=numpy.mat(Cluster_centre)
L=numpy.sum(Cluster_centre_matrix,axis=0)
M=L[0]*(1.0/len(Cluster_centre))
return numpy.array(M)[0]
def K_mean_plus_mean(points , cluster_centers):
"""初始化聚类中心,并将点分到这些聚类中心"""
changed = 0
wordLists=[list() for _ in xrange(len(cluster_centers))]
while True:
for p in points:
p.group = nearest_cluster_center(p, cluster_centers)[0]
wordLists[p.group].append(p.SentVec)
for i,cc in enumerate(cluster_centers):
cc.SentVec=comp_cluster_centers(wordLists[i])
changed = 0
for p in points:
min_i = nearest_cluster_center(p, cluster_centers)[0]
if min_i != p.group:
changed += 1
p.group = min_i
if changed ==0:
break
for i, cc in enumerate(cluster_centers):
cc.group = i
def SSE_Points(points,cluster_centers):
Sum_dis=0.0
for p in points:
Distance=(1-similarity(p.SentVec, cluster_centers[p.group].SentVec))**2
# print Distance
Sum_dis=Sum_dis+Distance
return Sum_dis
def test(k=7):
#initialize points information
points = generate_points()
#initialize K_cluster_centre
cluster_centers = [Point() for _ in xrange(k)]
K_findseed(points, cluster_centers)
#compute K_mean
K_mean_plus_mean(points,cluster_centers)
#compute SSE(sum of the squared errors)
SSE_sum=SSE_Points(points, cluster_centers)
# print("%s : %.5f" % ("SSE", SSE_sum))
return SSE_sum,points,cluster_centers
def test_show(k):
t0=time()
Min_SSE=numpy.inf
for i in range(20):
sse,ps,cs=test(k)
if sse < Min_SSE:
Min_SSE=sse
best_Cluster=ps
best_Cluster_centre=cs
print "best cluster result:"
print Min_SSE
Group_list=[list() for _ in xrange(k)]
for i in range(len(best_Cluster_centre)):
# print best_Cluster_centre[i].Sent,best_Cluster_centre[i].group
print u"-------这里输出第",i,u"类文本的句子------"
for p in best_Cluster:
if(int(p.number)==i):
print p.Sent,p.group,p.number
Group_list[i].append(str(p.group))
print ("%s :%.5fS"%("spend times",time()-t0))
return best_Cluster,Group_list
#compute entropy
def cacShannonEnt(dataset):
numEntries = len(dataset)
labelCounts = {}
for currentLabel in dataset:
if currentLabel not in labelCounts.keys():
labelCounts[currentLabel] = 0
labelCounts[currentLabel] +=1
shannonEnt = 0.0
for key in labelCounts:
prob = float(labelCounts[key])/numEntries
shannonEnt -= prob*math.log(prob, 2)
return shannonEnt
def test_entropy(best_Cluster,k,gl):
sum_entropy=0.0
for i in range(k):
print "entropy:",cacShannonEnt(gl[i])
sum_entropy=sum_entropy+((len(gl[i])*1.0)/len(best_Cluster))*cacShannonEnt(gl[i])
print sum_entropy
#compute purity
def cacpurityEnt(dataset):
numEntries = len(dataset)
labelCounts = {}
for currentLabel in dataset:
if currentLabel not in labelCounts.keys():
labelCounts[currentLabel] = 0
labelCounts[currentLabel] +=1
purityEnt = 0.0
for key in labelCounts:
prob = float(labelCounts[key])/numEntries
if prob > purityEnt:
purityEnt = prob
return purityEnt
def test_purity(best_Cluster,k,gl):
sum_purity=0.0
for i in range(k):
print "purity:",cacpurityEnt(gl[i])
sum_purity=sum_purity+((len(gl[i])*1.0)/len(best_Cluster))*cacpurityEnt(gl[i])
print sum_purity
#当test.txt改变执行
# filename="data/MyVec_Rad.pkl"
# points_Vec_Rad('data/test_10.txt')
filename="data/MyVec_add.pkl"
points_Vec_Add('data/test_10.txt')
bps,gl = test_show(k=10)
test_entropy(bps,10,gl)
test_purity(bps,10,gl)